24.07.2018 Views

Bash-Beginners-Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12. Catching signals<br />

In this chapter, we will discuss the following subjects:<br />

12.1. Signals<br />

♦ Available signals<br />

♦ Use of the signals<br />

♦ Use of the trap statement<br />

♦ How to prevent users from interrupting your programs<br />

12.1.1. Introduction<br />

12.1.1.1. Finding the signal man page<br />

Your system contains a man page listing all the available signals, but depending on your operating system, it<br />

might be opened in a different way. On most Linux systems, this will be man 7 signal. When in doubt, locate<br />

the exact man page and section using commands like<br />

man -k signal | grep list<br />

or<br />

apropos signal | grep list<br />

Signal names can be found using kill -l.<br />

12.1.1.2. Signals to your <strong>Bash</strong> shell<br />

In the absence of any traps, an interactive <strong>Bash</strong> shell ignores SIGTERM and SIGQUIT. SIGINT is caught and<br />

handled, and if job control is active, SIGTTIN, SIGTTOU and SIGTSTP are also ignored. Commands that are<br />

run as the result of a command substitution also ignore these signals, when keyboard generated.<br />

SIGHUP by default exits a shell. An interactive shell will send a SIGHUP to all jobs, running or stopped; see<br />

the documentation on the disown built-in if you want to disable this default behavior for a particular process.<br />

Use the huponexit option for killing all jobs upon receiving a SIGHUP signal, using the shopt built-in.<br />

12.1.1.3. Sending signals using the shell<br />

The following signals can be sent using the <strong>Bash</strong> shell:<br />

Table 12-1. Control signals in <strong>Bash</strong><br />

Standard key<br />

combination<br />

Ctrl+C<br />

Meaning<br />

The interrupt signal, sends SIGINT to the job running in the foreground.<br />

Chapter 12. Catching signals 137

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!