15

I wish to run a script on the remote system and then wish to stay there. Running following script:-

ssh user@remote logs.sh

This do run the script but after that I am back to my host system. i need to stay on remote one. I tried with..

ssh user@remote logs.sh;bash -l

somehow it solves the problem but still not working exactly as a fresh login as the command:-

ssh user@remote

Or it will be better if i could include something in my script that would open the bash terminal in the same directory where the script was running. Please suggest.

3
  • Can't you just do ssh user@remote logs.sh; ssh user@remote? Commented Feb 6, 2015 at 7:05
  • What do you mean by "not working exactly as a fresh login"? Commented Feb 6, 2015 at 7:06
  • it didnt work for me. indeed, the first command (logs) was ran correctly, but as soon as I pressed CTRL-C to get to the terminal it broke the SSH connection altogether Commented Jan 9, 2023 at 8:50

1 Answer 1

19

Try this:

ssh -t user@remote 'logs.sh; bash -l'

The quotes are needed to pass both commands to ssh. The -t option forces a pseudo-tty allocation.

Discussion

Consider:

ssh user@remote logs.sh;bash -l

When the shell parses this line, it splits it into two commands. The first is:

ssh user@remote logs.sh

This runs logs.sh on the remote machine. The second command is:

bash -l

This opens a login shell on the local machine.

The quotes were added above to prevent the shell from splitting up the commands this way.

Sign up to request clarification or add additional context in comments.

4 Comments

Throw in a -t to allocate a pty because he's trying to get an interactive shell rather than just running a command; and if you pass commands to ssh, it doesn't allocate a tty by default
@Petesh Thanks. bash -i worked but ssh -t works much better. Answer updated.
In addition -- To run a command after your ssh sessions finishes: ssh -t user@remote 'logs.sh; bash -l'; echo "--[remote session done]--";. That message prints after you exit. Or you can have whatever else you fancy.
what if the shell script is not on the remote machine, how can we run the shell script from local machine to the remote without uploading?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.