Signals and Traps
When writing shell scripts, and you’re creating tmp
files or doing anything that needs to or should be cleaned up either after the script is finished or in the event of an error, you should include a “cleanup” function and possibly an “error” function that first calls the “cleanup” function and presents an error code or message.
The main way of doing this is by the trap
function/command. Basically, it “traps” any exit codes/commands/signals presented by the script, by the user or by the system.
Below is the various signal names or number codes you should include in your trap line.
No |
Name |
Default Action |
Description |
---|---|---|---|
1 |
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|