1

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

asked Sep 27, 2013 at 17:20
1

2 Answers 2

5

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.

answered Sep 27, 2013 at 17:47
0

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/

answered Sep 27, 2013 at 20:37
1
  • This is way too complicated and error-prone compared to nohup. Commented Dec 4, 2020 at 15:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.