No, a background process group (job) is NOT killed by default when the session leader (the shell) exits, or when its controlling terminal is torn down.
There are only some special cases when that happens:
(1) The background job is stopped, in which case it will be sent a SIGHUP
/SIGCONT
pair of signals by the kernel. If the SIGHUP
signal is not caught or ignored by a process, the process will terminate.
The definition of a stopped job is: any job containing a stopped process. A process sleeping on a blocking system call like nanosleep(2)
or read(2)
is NOT considered stopped.
(2) A process tries to read or write to the terminal which no longer exists, and exits (of its own volition) because of the errors it gets when trying to do so.
(3) The job is actually a foreground job. The kernel sends a SIGHUP
signal to the foreground process group when the session leader / controlling process (ie the shell) terminates. The controlling process is itself signaled with SIGHUP
when its controlling terminal is torn down, which usually causes it to terminate.
Even commands started with &
are actually part of the foreground process group when they're started from a shell with no job control (which in most shells --but not in csh-- is the default when running scripts and subshells).
(4) You're using a shell like bash
or zsh
, which goes out of its way to send a SIGHUP
signal to all its jobs when it's itself signaled with SIGHUP
(per point 3. above, the shell being the controlling process), or simply when it exits (the latter only the default in zsh
, but not the default and subject to the shopt huponexit
option in bash).
The csh shell (either the real csh
or tcsh
) does not have that behaviour from bash
or zsh
. In tcsh
(but NOT in the real csh
) you can start a command with the hup
builtin in order to have it hup'ed when the shell exits:
tcsh% hup sleep 3600 &
tcsh% exit
$ pgrep sleep
[nothing]
(5) Your init system goes out of its way to cleanup any terminated user sessions. In its default configuration, systemd will signal all the processes from a scope with a SIGTERM
followed by a SIGKILL
after a delay, so nohup
will NOT help you with that anyway. Also, systemd's idea of a scope doesn't match a Unix process session, so running a command with setsid(1)
will not let it escape it, either.
You can probably change systemd's behaviour by tweaking away from their defaults the KillUserProcesses=yes
, KillMode=control-group
, KillSignal=SIGTERM
and SendSIGKILL=yes
options.
nohup
is a builtin in csh, and does NOT do some of the actions that a standalonenohup
program may do (like redirect stdout and stderr, and maybe even stdin). It's a perfectly fine idea to read thecsh(1)
manpage, too. And in general,nohup
is not some magic tool -- it just starts a program withSIGHUP
ignored. The standalonenohup
is also subject to nasty races. – Apr 07 '20 at 18:38ssh -t localhost csh
, and inside the csh, runsleep 3600 &
followed bykill -TSTP $!
, and then either exit the shell (ignoring the warning about stopped jobs) or break the ssh connection by pressing<Enter>
,~
and.
. The stoppedsleep
process will be killed. Thence, the case (1) applies to csh, too. – Apr 08 '20 at 22:09Also, I'm concerned about the ssh from the origin host getting killed (machine going down). The background task is still there on the destination host, without explicit nohup.
– neo_coder Apr 09 '20 at 00:42exit
again. Or simply break the ssh connection with~.
at the beginning of the line, as described (no, that won't kill the ssh server ;-)) Instead of ssh you can use script(1), a terminal emulator, a tmux or screen window, etc. – Apr 09 '20 at 00:45For non-interactive ssh sessions like the one I mentioned in "1.": ssh host 'sleep 80 >& /dev/null &'. Is nohup needed?
Regarding the concern about the ssh from the origin host getting killed (machine going down). The background task is still there on the destination host, without explicit nohup. Does that mean nohup is not needed in this scenario? – neo_coder Apr 09 '20 at 01:42
-t
option. – Apr 09 '20 at 02:52&
syntax should be used to spawn long-running, non-interactive processes. – Apr 09 '20 at 02:58