You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />
maud: ~> kill -9 5607<br />
maud: ~> ps -ef | grep stuck_process<br />
maud 5614 2214 0 20:15 pts/5 00:00:00 grep stuck_process<br />
[1]+ Killed stuck_process<br />
When a process starts up several instances, killall might be easier. It takes the same option as the kill<br />
command, but applies on all instances of a given process. Test this command before using it in a production<br />
environment, since it might not work as expected on some of the commercial Unices.<br />
12.2. Traps<br />
12.2.1. General<br />
There might be situations when you don't want users of your scripts to exit untimely using keyboard abort<br />
sequences, for example because input has to be provided or cleanup has to be done. The trap statement<br />
catches these sequences and can be programmed to execute a list of commands upon catching those signals.<br />
The syntax for the trap statement is straightforward:<br />
trap [COMMANDS] [SIGNALS]<br />
This instructs the trap command to catch the listed SIGNALS, which may be signal names with or without the<br />
SIG prefix, or signal numbers. If a signal is 0 or EXIT, the COMMANDS are executed when the shell exits. If<br />
one of the signals is DEBUG, the list of COMMANDS is executed after every simple command. A signal<br />
may also be specified as ERR; in that case COMMANDS are executed each time a simple command exits<br />
with a non-zero status. Note that these commands will not be executed when the non-zero exit status comes<br />
from part of an if statement, or from a while or until loop. Neither will they be executed if a logical AND<br />
(&&) or OR (||) result in a non-zero exit code, or when a command's return status is inverted using the !<br />
operator.<br />
The return status of the trap command itself is zero unless an invalid signal specification is encountered. The<br />
trap command takes a couple of options, which are documented in the <strong>Bash</strong> info pages.<br />
Here is a very simple example, catching Ctrl+C from the user, upon which a message is printed. When you<br />
try to kill this program without specifying the KILL signal, nothing will happen:<br />
#!/bin/bash<br />
# traptest.sh<br />
trap "echo Booh!" SIGINT SIGTERM<br />
echo "pid is $$"<br />
while :<br />
do<br />
done<br />
sleep 60<br />
# This is the same as "while true".<br />
# This script is not really doing anything.<br />
12.2.2. How <strong>Bash</strong> interprets traps<br />
When <strong>Bash</strong> receives a signal for which a trap has been set while waiting for a command to complete, the trap<br />
will not be executed until the command completes. When <strong>Bash</strong> is waiting for an asynchronous command via<br />
the wait built-in, the reception of a signal for which a trap has been set will cause the wait built-in to return<br />
Chapter 12. Catching signals 139