summaryrefslogtreecommitdiff
path: root/devdocs/bash/signals.html
blob: b5cb68850d4bf34345a7f271e498ec83387bcbde (plain)
1
2
3
4
5
6
7
8
<h1 class="subsection">Signals</h1>  <p>When Bash is interactive, in the absence of any traps, it ignores <code>SIGTERM</code> (so that ‘<samp>kill 0</samp>’ does not kill an interactive shell), and <code>SIGINT</code> is caught and handled (so that the <code>wait</code> builtin is interruptible). When Bash receives a <code>SIGINT</code>, it breaks out of any executing loops. In all cases, Bash ignores <code>SIGQUIT</code>. If job control is in effect (see <a href="job-control">Job Control</a>), Bash ignores <code>SIGTTIN</code>, <code>SIGTTOU</code>, and <code>SIGTSTP</code>. </p> <p>Non-builtin commands started by Bash have signal handlers set to the values inherited by the shell from its parent. When job control is not in effect, asynchronous commands ignore <code>SIGINT</code> and <code>SIGQUIT</code> in addition to these inherited handlers. Commands run as a result of command substitution ignore the keyboard-generated job control signals <code>SIGTTIN</code>, <code>SIGTTOU</code>, and <code>SIGTSTP</code>. </p> <p>The shell exits by default upon receipt of a <code>SIGHUP</code>. Before exiting, an interactive shell resends the <code>SIGHUP</code> to all jobs, running or stopped. Stopped jobs are sent <code>SIGCONT</code> to ensure that they receive the <code>SIGHUP</code>. To prevent the shell from sending the <code>SIGHUP</code> signal to a particular job, it should be removed from the jobs table with the <code>disown</code> builtin (see <a href="job-control-builtins">Job Control Builtins</a>) or marked to not receive <code>SIGHUP</code> using <code>disown -h</code>. </p> <p>If the <code>huponexit</code> shell option has been set with <code>shopt</code> (see <a href="the-shopt-builtin">The Shopt Builtin</a>), Bash sends a <code>SIGHUP</code> to all jobs when an interactive login shell exits. </p> <p>If Bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes. When Bash is waiting for an asynchronous command via the <code>wait</code> builtin, the reception of a signal for which a trap has been set will cause the <code>wait</code> builtin to return immediately with an exit status greater than 128, immediately after which the trap is executed. </p> <p>When job control is not enabled, and Bash is waiting for a foreground command to complete, the shell receives keyboard-generated signals such as <code>SIGINT</code> (usually generated by ‘<samp>^C</samp>’) that users commonly intend to send to that command. This happens because the shell and the command are in the same process group as the terminal, and ‘<samp>^C</samp>’ sends <code>SIGINT</code> to all processes in that process group. See <a href="job-control">Job Control</a>, for a more in-depth discussion of process groups. </p> <p>When Bash is running without job control enabled and receives <code>SIGINT</code> while waiting for a foreground command, it waits until that foreground command terminates and then decides what to do about the <code>SIGINT</code>: </p> <ol> <li> If the command terminates due to the <code>SIGINT</code>, Bash concludes that the user meant to end the entire script, and acts on the <code>SIGINT</code> (e.g., by running a <code>SIGINT</code> trap or exiting itself); </li>
<li> If the pipeline does not terminate due to <code>SIGINT</code>, the program handled the <code>SIGINT</code> itself and did not treat it as a fatal signal. In that case, Bash does not treat <code>SIGINT</code> as a fatal signal, either, instead assuming that the <code>SIGINT</code> was used as part of the program’s normal operation (e.g., <code>emacs</code> uses it to abort editing commands) or deliberately discarded. However, Bash will run any trap set on <code>SIGINT</code>, as it does with any other trapped signal it receives while it is waiting for the foreground command to complete, for compatibility. </li>
</ol><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.<br>Licensed under the GNU Free Documentation License.<br>
    <a href="https://www.gnu.org/software/bash/manual/html_node/Signals.html" class="_attribution-link">https://www.gnu.org/software/bash/manual/html_node/Signals.html</a>
  </p>
</div>