Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH : Remotely run a script and stay there

Tags:

shell

unix

ssh

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.

like image 214
ratednitesh Avatar asked Feb 06 '15 07:02

ratednitesh


People also ask

How do you run a script via SSH that doesn't end when you close the connection?

You can run the command with the nohup command before it. You can also run it in 'screen', which will allow you reattach the terminal. Or just ssh into and run the nohup command. It should keep running even when you disconnect.

How do I run a script on a remote server?

To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. The script must be on or accessible to your local computer. The results are returned to your local computer.


1 Answers

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.

like image 84
John1024 Avatar answered Oct 04 '22 17:10

John1024