10

I read an answer from a user who claimed that running

foo 2>&1 >& output.log &

would result in foo continuing to run even when they log out. According to this user, this even worked over SSH connections.

I didn't really believe that, as I was under the impression that in the case of disconnecting from SSH, or terminating the TTY, the shell and therefore its processes would receive a SIGHUP, causing them to terminate. This, under my assumption, was the sole reason for using nohup in such cases, or tmux, screen et al.

I then looked into glibc's manual:

This signal is also used to report the termination of the controlling process on a terminal to jobs associated with that session; this termination effectively disconnects all processes in the session from the controlling terminal.

This seems to confirm my thoughts. But looking further, it says:

If the process is a session leader that has a controlling terminal, then a SIGHUP signal is sent to each process in the foreground job, and the controlling terminal is disassociated from that session.

So, does this mean jobs put in background will not receive SIGHUP?

To my further confusion, I ran an interactive Zsh session, ran yes >& /dev/null &, and typed exit, when Zsh warned me that I had running jobs, and after typing exit for a second time, told me that it had SIGHUPed one job. Doing exactly the same in Bash leaves the job running…

  • I've had problems with a server in the past that gets suddenly disconnected over ssh, and my process just kept running but without any tty associated, so I had to login again and send SIGHUP/TERM/KILL to end them. So, following the same logic, I have tested just now, typed logout and yes is still running. – Braiam Jul 28 '13 at 21:08
  • In addition to whether or not SIGHUP is sent, whether a process has defined a signal handler (and catches SIGHUP) or signal mask are additional factors in whether it is terminated when sent that signal. So just because it remains running after logout doesn't mean it wasn't sent that signal – Tim B Aug 2 '13 at 13:05
  • Are you referring to this question: serverfault.com/questions/115999/… ? The answer seems to be found there - it's RedHat not setting shopt by default. It's RedHat's particular configuration quirk. – Boris Burkov Aug 2 '13 at 15:07
  • @Bob No, I haven't seen this one before, but it's a good resource. Thanks! – slhck Aug 2 '13 at 15:21
  • Glad it helped. Actually Raphael is referring to that issue too. – Boris Burkov Aug 2 '13 at 15:28
17
+50

Bash seems to send the SIGHUP only if it self received a SIGHUP, which for example occurs when a virtual terminal is closed or when the SSH connection is interrupted. From the documentation:

The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends the SIGHUP to all jobs, running or stopped. Stopped jobs are sent SIGCONT to ensure that they receive the SIGHUP. To prevent the shell from sending the SIGHUP signal to a particular job, it should be removed from the jobs table with the disown builtin (see Job Control Builtins) or marked to not receive SIGHUP using disown -h.

So if you type exit or press Ctrl+D all background process will remain, since this does not send a hang up signal to the Bash.

You can force Bash to warn you about the fact that there are still running background processes with

shopt -s checkjobs

There is an option to send the SIGHUP on exit if you are in an interactive shell(see here). But it doesn't work on my machine with Bash 4.2.25. Maybe it works for you

shopt -s huponexit

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.