Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use SSH to start a background process on a remote server, and exit session

I am using SSH to start a background process on a remote server. This is what I have at the moment:

ssh [email protected] "nohup process &"

This works, in that the process does start. But the SSH session itself does not end until I hit Ctr-C.

When I hit Ctr-C, the remote process continues to run in the background.

I would like to place the ssh command in a script that I can run locally, so I would like the ssh session to exit automatically once the remote process has started.

Is there a way to make this happen?

like image 772
futureshocked Avatar asked Nov 15 '13 07:11

futureshocked


People also ask

How do I start a process over SSH and disconnect?

ssh into your remote box. type screen Then start the process you want. Press Ctrl-A then Ctrl-D. This will detach your screen session but leave your processes running.

How do I SSH in the background?

To execute SSH command remotely in the background without this issue, you need to prevent SSH from blocking for input by redirecting the output to /dev/null. You will then also need to send your SSH client session to the background. You can achieve both effects by using the -n and -f options for the SSH client.

How do I keep a program running after closing SSH session?

ssh into your remote box. Type screen Then start the process you want. Press Ctrl-A then Ctrl-D . This will "detach" your screen session but leave your processes running.


1 Answers

The "-f" option to ssh tells ssh to run the remote command in the background and to return immediately. E.g.,

ssh -f user@host "echo foo; sleep 5; echo bar" 

If you type the above, you will get your shell prompt back immediately, you will then see "foo" output. Five seconds later you will then see "bar" output. In the meantime, you could have been using the shell.

like image 108
Douglas Avatar answered Sep 22 '22 12:09

Douglas