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

futureshocked picture futureshocked · Nov 15, 2013 · Viewed 57.3k times · Source

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?

Answer

Douglas picture Douglas · Mar 12, 2014

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.