Introduction
A shell script can run into problems during its execution, resulting in an error signal that interrupts the script unexpectedly.
Errors occur due to a faulty script design, user actions, or system failures. A script that fails may leave behind temporary files that cause trouble when a user restarts the script.
This tutorial will show you how to use the trap
command to ensure your scripts always exit predictably.
Prerequisites
- Access to the terminal/command line.
- A text editor (Nano, Vi/Vim, etc.).
Bash trap Syntax
The syntax for the trap
command is:
trap [options] "[arguments]" [signals]
The command has the following components:
- Options provide added functionality to the command.
- Arguments are the commands
trap
executes upon detecting a signal. Unless the command is only one word, it should be enclosed with quotation marks (" "
). If the argument contains more than one command, separate them with a semicolon (;
). - Signals are asynchronous notifications sent by the system, usually indicating a user-generated or system-related interruption. Signals can be called by their name or number.
Bash trap Options
The trap
command accepts the following options:
-p
– Displays signal commands.-l
– Prints a list of all the signals and their numbers.
Below is the complete list of the 64 signals and their numbers:
# | Signal | # | Signal | # | Signal |
---|---|---|---|---|---|
1 | SIGHUP | 23 | SIGURG | 45 | SIGRTMIN+11 |
2 | SIGINT | 24 | SIGXCPU | 46 | SIGRTMIN+12 |
3 | SIGQUIT | 25 | SIGXFSZ | 47 | SIGRTMIN+13 |
4 | SIGILL | 26 | SIGVTALRM | 48 | SIGRTMIN+14 |
5 | SIGTRAP | 27 | SIGPROF | 49 | SIGRTMIN+15 |
6 | SIGABRT | 28 | SIGWINCH | 50 | SIGRTMAX-14 |
7 | SIGBUS | 29 | SIGIO | 51 | SIGRTMAX-13 |
8 | SIGFPE | 30 | SIGPWR | 52 | SIGRTMAX-12 |
9 | SIGKILL | 31 | SIGSYS | 53 | SIGRTMAX-11 |
10 | SIGUSR1 | 32 | SIGWAITING | 54 | SIGRTMAX-10 |
11 | SIGSEGV | 33 | SIGLWP | 55 | SIGRTMAX-9 |
12 | SIGUSR2 | 34 | SIGRTMIN | 56 | SIGRTMAX-8 |
13 | SIGPIPE | 35 | SIGRTMIN+1 | 57 | SIGRTMAX-7 |
14 | SIGALRM | 36 | SIGRTMIN+2 | 58 | SIGRTMAX-6 |
15 | SIGTERM | 37 | SIGRTMIN+3 | 59 | SIGRTMAX-5 |
16 | SIGSTKFLT | 38 | SIGRTMIN+4 | 60 | SIGRTMAX-4 |
17 | SIGCHLD | 39 | SIGRTMIN+5 | 61 | SIGRTMAX-3 |
18 | SIGCONT | 40 | SIGRTMIN+6 | 62 | SIGRTMAX-2 |
19 | SIGSTOP | 41 | SIGRTMIN+7 | 63 | SIGRTMAX-1 |
20 | SIGTSTP | 42 | SIGRTMIN+8 | 64 | SIGRTMAX |
21 | SIGTTIN | 43 | SIGRTMIN+9 | ||
22 | SIGTTOU | 44 | SIGRTMIN+10 |
Note: Signals 32 and 33 are not supported in Linux, and the trap -l
command does not display them in the output.
The signals most commonly used with the trap
command are:
SIGHUP (1)
– Clean tidy-upSIGINT (2)
– InterruptSIGQUIT (3)
– QuitSIGABRT (6)
– CancelSIGALRM (14)
– Alarm clockSIGTERM (15)
– Terminate
Note: The SIG
prefix in signal names is optional. For example, SIGTERM
signal can also be written as TERM
.
How to Use trap in Bash
A typical scenario for using the trap
command is catching the SIGINT
signal. This signal is sent by the system when the user interrupts the execution of the script by pressing Ctrl+C.
The following example script prints the word “Test” every second until the user interrupts it with Ctrl+C. The script then prints a message and quits.
trap "echo The script is terminated; exit" SIGINT
while true
do
echo Test
sleep 1
done
The while
loop in the example above executes infinitely. The first line of the script contains the trap
command and the instructions to wait for the SIGINT
signal, then print the message and exit the script.
The trap
command is frequently used to clean up temporary files if the script exits due to interruption. The following example defines the cleanup
function, which prints a message, removes all the files added to the $TRASH
variable, and exits the script.
$TRASH=$(mktemp -t tmp.XXXXXXXXXX)
trap cleanup 1 2 3 6
cleanup()
{
echo "Removing temporary files:"
rm -rf "$TRASH"
exit
}
...
The trap
in the example above executes the cleanup
function when it detects one of the four signals: SIGHUP
, SIGINT
, SIGQUIT
, or SIGABRT
. The signals are referred to by their number.
You can also use trap
to ensure the user cannot interrupt the script execution. This feature is important when executing sensitive commands whose interruption may permanently damage the system. The syntax for disabling a signal is:
trap "" [signal]
Double quotation marks mean that no command will be executed. For example, to trap the SIGINT
and SIGABRT
signals, type:
trap "" SIGINT SIGABRT
[a command that must not be interrupted]
If you wish to re-enable the signals at any time during the script, reset the rules by using the dash symbol:
trap - SIGINT SIGABRT
[a command that can be interrupted]
Note: The SIGKILL
signal cannot be trapped. It always immediately interrupts the script.
Conclusion
After reading this tutorial, you know how to use the trap
command to ensure your bash script always exits properly. If you are interested in more Bash-related topics, read How to Run a Bash Script.
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/223463.html