0

I got a script that I'm trying to run with nohup:

nohup /home/user/script.sh&

If I run ps -a I can still see the process but if I close the terminal and run ps -a again the process is not there. Is this supposed to happen? I would like to run the script in the background.

Thanks

  • Are you using systemd? If so, do you have KillUserProcesses=no in your /etc/systemd/logind.conf? If not, then systemd sends a SIGKILL instead of a SIGHUP to all of your login session's remaining processes when you log out. NOTE: KillUserProcesses=yes is the systemd default, but some distributions like debian and fedora compile systemd with that disabled. – cas Sep 13 '19 at 4:00
  • there is, of course, no need for systemd to do this (they'll get killed anyway unless you run them with nohup or similar) - but Lennart always knows best. – cas Sep 13 '19 at 4:04
  • nohup will not prevent against your script dying because of an EIO error when trying to write to the tty. Or an EOF or EIO error -- depending on the system -- when trying to read from it. nohup does redirect the standard fds of the program away from the tty, but does not dissociate it from the tty; the process can still access it via /dev/tty, eg. nohup sh -c 'while echo $$ alive; do sleep 1; done >/dev/tty' &. Try to reduce your script to a small example. If you're on Linux, you may just use setsid instead of nohup. – mosvy Sep 13 '19 at 4:44
0

That's because the process is spawned via that terminal you're currently in and your & is putting it into the background of that terminal.

Do a ps faux and you can see the family tree of the process and note how it is spawned from the terminal you're currently in. If you kill that terminal process, you're killing all the child processes as well, such as your currently running script.

Alternatives are to use something like tmux or screen to run the script in a background session, run it as a cronjob, or daemonize it via your init system by creating a unit file for it.

| improve this answer | |

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.