< Day Day Up > |
14.8. Trapping SignalsWhile your program is running, if you press Ctrl-C or Ctrl-\, your program terminates as soon as the signal arrives. There are times when you would rather not have the program terminate immediately after the signal arrives. You could arrange to ignore the signal and keep running or perform some sort of cleanup operation before actually exiting the script. The trap command allows you to control the way a program behaves when it receives a signal. A signal is defined as an asynchronous message that consists of a number that can be sent from one process to another, or by the operating system to a process if certain keys are pressed or if something exceptional happens.[6] The trap command tells the shell to terminate the command currently in execution upon the receipt of a signal. If the trap command is followed by commands within quotes, the command string will be executed upon receipt of a specified signal. The shell reads the command string twice, once when the trap is set, and again when the signal arrives. If the command string is surrounded by double quotes, all variable and command substitution will be performed when the trap is set the first time. If single quotes enclose the command string, variable and command substitution do not take place until the signal is detected and the trap is executed.
Use the command kill –l or trap –l to get a list of all signals. Table 14.6 provides a list of signal numbers and their corresponding names. The most commonly used signals for trap are 1) SIGHUP (hangup), 2) SIGINT (interrupt), 3) SIGQUIT (quit), and 4) SIGTERM (exit).
FORMAT trap 'command; command' signal-number trap 'command; command' signal-name Example 14.61.
trap 'rm tmp*; exit 1' 0 1 2 15
trap 'rm tmp*; exit 1' EXIT HUP INT TERM
EXPLANATION When any of the signals 1 (hangup), 2 (interrupt), or 15 (software termination) arrives, remove all the tmp files and exit. If an interrupt comes in while the script is running, the trap command lets you handle the interrupt signal in several ways. You can let the signal behave normally (default), ignore the signal, or create a handler function to be called when the signal arrives. Signal names such as HUP and INT are normally prefixed with SIG, for example, SIGHUP, SIGINT, and so forth.[7] The bash shell allows you to use symbolic names for the signals, which are the signal names without the SIG prefix, or you can use the numeric value for the signal. See Table 14.6. A pseudo signal name EXIT, or the number 0, will cause the trap to be executed when the shell exits.
14.8.1 Resetting SignalsTo reset a signal to its default behavior, the trap command is followed by the signal name or number. Traps set in functions are recognized by the shell that invoked the function, once the function has been called. Any traps set outside the function are also recognized with the function. Example 14.62.
trap INT
EXPLANATION Resets the default action for signal 2, SIGINT. The default action is to kill the process when the interrupt key (Ctrl-C) is pressed. Example 14.63.
trap 2
EXPLANATION Resets the default action for signal 2, SIGINT, which is used to kill a process (i.e., Ctrl-C). Example 14.64.
trap 'trap 2' 2
EXPLANATION Sets the default action for signal 2 (SIGINT) to execute the command string within quotes when the signal arrives. The user must press Ctrl-C twice to terminate the program. The first trap catches the signal, and the second trap resets the trap back to its default action, which is to kill the process. 14.8.2 Ignoring SignalsIf the trap command is followed by a pair of empty quotes, the signals listed will be ignored by the process. Example 14.65.trap " " 1 2 or trap "" HUP INT EXPLANATION Signals 1 (SIGHUP) and 2 (SIGINT) will be ignored by the shell process. 14.8.3 Listing TrapsTo list all traps and the commands assigned to them, type trap. Example 14.66.
(At the command line)
1 $ trap 'echo "Caught ya!; exit"' 2
2 $ trap
trap -- 'echo "Caught ya!; exit 1"' SIGINT
3 $ trap -
EXPLANATION
Example 14.67.(The Script) #!/bin/bash # Scriptname: trapping # Script to illustrate the trap command and signals # Can use the signal numbers or bash abbreviations seen # below. Cannot use SIGINT, SIGQUIT, etc. 1 trap 'echo "Ctrl-C will not terminate $0."' INT 2 trap 'echo "Ctrl-\\ will not terminate $0."' QUIT 3 trap 'echo "Ctrl-Z will not terminate $0."' TSTP 4 echo "Enter any string after the prompt. When you are ready to exit, type \"stop\"." 5 while true do 6 echo –n "Go ahead...> " 7 read 8 if [[ $REPLY == [Ss]top ]] then 9 break fi 10 done (The Output) 4 Enter any string after the prompt. When you are ready to exit, type "stop". 6 Go ahead...> this is it^C 1 Ctrl-C will not terminate trapping. 6 Go ahead...> this is it again^Z 3 Ctrl-Z will not terminate trapping. 6 Go ahead...> this is never it|^\ 2 Ctrl-\\ will not terminate trapping. 6 Go ahead...> stop $ EXPLANATION
14.8.4 Traps and FunctionsIf you use a trap to handle a signal in a function, it will affect the entire script, once the function is called. The trap is global to the script. In the following example, the trap is set to ignore the interrupt key, ^C. This script had to be killed with the kill command to stop the looping. It demonstrates potential undesirable side effects when using traps in functions. Example 14.68.(The Script) #!/bin/bash 1 function trapper () { echo "In trapper" 2 trap 'echo "Caught in a trap!"' INT # Once set, this trap affects the entire script. Anytime # ^C is entered, the script will ignore it. } 3 while : do echo "In the main script" 4 trapper 5 echo "Still in main" 6 echo "The pid is $$" sleep 5 7 done (The Output) In the main script In trapper Still in main The pid is 4267 Caught in a trap! # User presses ^C In the main script In trapper Still in main The pid is 4267 Caught in a trap! # User just pressed Ctrl-C In the main script EXPLANATION
|
< Day Day Up > |