14

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.

ratednitesh
  • 248
  • 1
  • 2
  • 5

1 Answers1

17

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.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • 4
    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 – Anya Shenanigans Feb 06 '15 at 07:52
  • @Petesh Thanks. `bash -i` worked but `ssh -t` works much better. Answer updated. – John1024 Feb 06 '15 at 08:01
  • 2
    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. – will Mar 19 '19 at 03:05
  • 1
    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? – laughing Jun 10 '20 at 00:58