Bash processing signal
Send Signal
Kill
Format: kill[-s Sigspec |-n signum |-sigspec] [PID | jobspec] ...
You can send a process with a process number of PID, or you can send a process with the job number Jobspec
Sigspec can be either a signal number or a signal name, and the signal name can be no sig
The KILL command sends the SIGTERM signal by default, and the process that receives the sigterm signal ends the run
Terminal, CTRL + C sends int signal to foreground process, process exits, exit status is 130, add 128 for signal number
Processing signal
Establishing a signal callback function
Trapcmd signal1 signal2 Signal3 ...
CMD can be a sequence of commands or a function's name, or it can be empty, which will perform the default behavior
Signals of interest to shell script programming
Signal |
Corresponding ID |
Behavior |
Meaning |
SIGHUP |
1 |
Term |
hangupdetected on controlling terminal |
SIGINT |
2 |
Term |
Interruptfrom keyboard |
Sigquit |
3 |
Core |
Quitfrom keyboard |
SIGKILL |
9 |
Term |
Killsignal |
Sigalrm |
14 |
Term |
Timersignal |
SIGTERM |
15 |
Term |
Terminationsignal |
The shell script has a false signal 0, which is generated at exit and can be used to define a function for this signal to perform certain actions at the end of the script
Define the operation for the same signal multiple times, whichever is the last
Ignore signal
Use an empty string or colon in the trap to line.
Trap ' 2//Note the existence of quotation marks
Set timer
In fact, set up a daemon in the background to send sigalarm signals to the process after a certain event of sleep.
Way: Sleep$time && kill-s $$ &
This command generates a sleep process from the bash subprocess after the bash subprocess.
Notice the sequence of killing process when unregistering the timer, kill the parent process rashly, can't find the child process OH
This article is from the "No Front" blog, please be sure to keep this source http://qianyang.blog.51cto.com/7130735/1620558
Bash Signal Processing