I want to start a shell script that resides on a remote machine. When I use

ssh user@example.com /path/to/script.sh

it needs the ssh connection to be open until the script has terminated.

But I need the script to continue to run, even after I close the ssh connection until the script finishes by itself. I just want to start a process and then forget about it.

How can I do this using ssh?

Thanks a lot!

P.S.: This is not a duplicate of this stackechange question.


I believe the problem is that when the SSH session is closed (by hitting ctrl-c, or closing the xterm), a HUP is sent to the process. To fork the process to background, add &, and to block the hup, use nohup:

ssh user@example.com 'nohup sleep 300 >/dev/null 2>/dev/null </dev/null &'

The ssh should start and the process will run on example.com in the background.

If you wish to monitor its progress, you can use screen, if you wish to do that then something similar to this will help:

ssh user@example.com -t 'screen -D -RR -S this /bin/sleep 300'

This creates a screen session named 'this' (-S), detaches an already running screen if attached elsewhere and reattaches here. Then starts /bin/sleep with a 5min wait.

  • If I wanted to save the output of the command to a file on the remote machine, where would the file path go (which dev/null should be replaced)? – Gary Czychi Feb 17 '17 at 22:50
  • >/dev/null redirects the output of STDOUT to /dev/null. 2>/dev/null redirects STDERR to /dev/null. </dev/null reads the input of /dev/null for the program's STDIN (i.e., nothing). If you want to capture the normal output of the program, change >/dev/null – dpw Feb 17 '17 at 23:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.