Use of Linux shell trap
1. After the trap captures the signal, there are three response methods:
(1) execute a program to process this signal.
(2) default operation for receiving signals
(3) Ignore this signal
Ii. trap provides three basic forms for the above three methods:
The first form of trap Command will execute the double
The command string in quotation marks.
Trap 'commands' signal-list
Trap "commands" signal-list
To restore the default signal, use the second form of trap command:
Trap signal-list
The third form of trap command allows ignoring Signals
Trap "" signal-list
Note:
(1) The signal 11 (segment violation) cannot be captured, because the shell itself needs to capture the signal for memory dump.
(2) In the trap, you can define the processing of the signal 0 (in fact, there is no such signal). The shell program sends this signal when it terminates (such as executing the exit Statement.
(3) After capturing the signal specified in the signal-list and executing the corresponding commands, if these commands do not terminate the shell program, the shell program will continue to execute the command after the command received when the signal is received, which will easily cause the shell program to be unable to terminate.
In the trap statement, single quotes and double quotation marks are different. When the shell program first encounters a trap statement, it will scan the commands in commands. If commands is enclosed in single quotes, shell will not replace the variables and commands in commands. Otherwise, the variables and commands in commands will be replaced with the specific values at that time.
Signal description
HUP (1) suspended, usually caused by terminal disconnection or user exit
INT (2) interruption, usually caused by pressing Ctrl + C
QUIT (3) exits, usually caused by pressing Ctrl + key combination
ABRT (6) abort, usually caused by some serious execution errors
ALRM (14) Alarm, usually used to handle timeout
TERM (15) Termination, usually sent when the system is shut down
This article permanently updates the link address: