When did HUP stop getting sent and what can I do about it?

久未见 提交于 2019-12-05 02:08:16

I believe you're looking for the huponexit shell option. You can set this easily with

$ shopt -s huponexit

Some details from the bash man page:

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 signal to a par- ticular job, it should be removed from the jobs table with the disown builtin (see SHELL BUILTIN COMMANDS below) or marked to not receive SIGHUP using disown -h.

If the huponexit shell option has been set with shopt, bash sends a SIGHUP to all jobs when an interactive login shell exits.

I still get a SIGHUP. A simple way to test:

#!/usr/bin/env sh

echo "$$"
trap "echo HUPPED $$ > /tmp/willithup" HUP
sleep 1000

Then close the terminal emulator. Now back to your question:

Watch it print the date every 10 seconds, then close the original parent shell with Ctrl-D. Still running. Log out and back in. Still running.

The process doesn't get a HUP when its parent dies. It gets a HUP when it loses the connection to the controlling terminal or when it is explicitly sent a HUP. This happens for example when you logout of SSH.

If we weren't careful, our processes would get killed when we logged off, or even closed the shell we started them from

For the second one: the shell itself can send a HUP to all its children when it exits. However, bash for example has huponexit set to false by default. This may well be what has changed. Note that regardless of the huponexit option, when it receives a HUP the shell also sends a HUP to all its children.


In the words of Stevens:

A session can have a single controlling terminal. This is usually the terminal device (in the case of a terminal login) or pseudo terminal device (in the case of a network login) on which we log in.

If a modem (or network) disconnect is detected by the terminal interface the hang-up signal is sent to the controlling process (the session leader).


To further clarify, the initial HUP is not sent by the shell. It is sent by the terminal driver. Afterwards the shell "forwards" it to the children. So indeed, a HUP sent by the terminal driver can "cascade". From TLPI:

When a controlling process loses its terminal connection, the kernel sends it a SIGHUP signal to inform it of this fact. (A SIGCONT signal is also sent, to ensure that the process is restarted in case it had been previously stopped by a signal.) Typically, this may occur in two circumstances:

  • When a "disconnect" is detected by the terminal driver, indicating a loss of signal on a modem or terminal line.
  • When a terminal window is closed on a workstation. This occurs because the last open file descriptor for the master side of the pseudoterminal associated with the terminal window is closed.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!