I have tried appending '&' but the script is still stopped when my remote connection is terminated


Use nohup ("no hangup"):

nohup myscript.sh </dev/null 1>&2&> nohup.log &

The </dev/null provides a standard input stream that won't close, since closing the that may confuse some things. This may or may not be necessary, since man nohup claims that's what it does anyway -- try it without and if that doesn't work, try it with.

The 1>&2&> nohup.log is optional; it just sends standard output and standard error to a file. If you don't do that, nohup will append the first one to nohup.out or $HOME/nohup.out.

As per usual with &, I find I have to hit the return key at least one extra time to get the prompt back.

An even better way to deal with this issue is setsid, which "reparents" the process by starting a new process session, meaning the parent is init (or systemd) rather than ssh. Init (systemd is an init implementation) is the first process started on the system (and the only one started by the kernel). It can never die (or, it is always restarted and adopts the children of the last instance), so its children are safe (this it how system services work). Normally, if a session leader (in this case ssh) exits, its children will be terminated shortly thereafter.

sudo setsid myscript.sh

Substituting whatever long running command you want for myscript.sh. If the command doesn't return immediately (it should), add a & at the end, or check in man setsid if your system supports the new --fork option.


You could use a crontab which will run a command at a given time. Just ssh in set up the command in the crontab to start in a minute or two and log out.

to access the crontab enter crontab -e in LXterminal

The instructions are in the file, if it is 21:00 now then enter 02 21 * * * command

the command will be started at 21:02 you can add the date otherwise it will start at 21:02 each day

I have an article explaining how to do this with the Raspberry pi camera for time lapse which may help. http://www.raspberryconnect.com/hardware-add-ons/item/137-raspberry-pi-camera-module

more info here as well http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

  • This is way too complicated and error-prone compared to nohup. Dec 4 '20 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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