In the shell, send a signal to the process a lot through the CTRL key plus some function keys to achieve, here is the common CTRL key combination and its meaning:
Key combinations |
Signal type |
Significance |
CTRL + C |
int signal, i.e. interrupt signal |
Stop running the current job |
CTRL + Z |
TSTP signal, i.e. terminal stop signal |
Temporarily stop the current job (into a blocking state) |
Ctrl+\ |
Quit signal |
The enhanced version of the CTRL + C signal, when CTRL + C cannot stop the job test, use the key combination |
Ctrl+y |
TSTP signal, i.e. terminal stop signal |
When the process reads input data from the terminal, it temporarily stops the process |
The child she will be able to inherit the signal that the parent she ll ignores, but cannot inherit the signal that the parent she ' Llwei does not ignore, and we illustrate this view with an example, creating two scripts forever.sh and subsig.sh,forever.sh content as follows:
#forever. SH demo infinite loop wait #!/bin/bashwhile do #冒号相当于true. Sleep 5 Done
The forever.sh script is a subsig.sh script call to create a child shell, the function is an infinite loop, each cycle sleep 5 seconds, that is, the forever.sh script will never stop, unless killed by the kill command. Subsig.sh content is as follows:
#!/bin/"" QUIT "Echo ' You want to kill me '" term # After capturing the term signal, print the prompt message (. /forever. SH #将forever. Sh script as a child shell, the child shell will sleep indefinitely)
View Code
Subsig.sh used two trap commands, ignoring the quit signal, but not ignoring the term signal, after capturing the term signal, you need to print the message, and then use parentheses to establish a child shell, the following is the parent-child shell processing quit and term signal testing process:
[Email protected] shellscript]$./subsig.SH&#运行subsig. sh script [1]5118#返回父shell作业号和进程号 [[email protected] shellscript] $ Kill-3 5118#向父she'll send signal No. 3rd, i.e. quit signal[Email protected] shellscript]$PS-a #父shell未退出, stating that the quit signal is ignored by PID TTY time CMD3822pts/0 xx:xx:xx su 3830pts/0 xx:xx:xxBash3868pts/0 xx:xx:xx su 3869pts/0 xx:xx:xxBash4037pts/0 xx:xx:xxVim4038pts/0 xx:xx:xxVim4418pts/0 xx:xx:xxTraploop.SH 4499pts/0 xx:xx:xx Sleep 5118pts/0 xx:xx:xxSubsig.SH 5119pts/0 xx:xx:xxForever.SH 5123pts/0 xx:xx:xx Sleep 5124pts/0 xx:xx:xx PS[email protected] shellscript]$Kill-3 5119#向子shell发送3信号, that is, quit signal [[email protected] shellscript]$PS-a #子shell也未退出, stating that the quit signal is also ignored by PID TTY time CMD3822pts/0 xx:xx:xx su 3830pts/0 xx:xx:xxBash3868pts/0 xx:xx:xx su 3869pts/0 xx:xx:xxBash4037pts/0 xx:xx:xxVim4038pts/0 xx:xx:xxVim4418pts/0 xx:xx:xxTraploop.SH 4499pts/0 xx:xx:xx Sleep 5118pts/0 xx:xx:xxSubsig.SH 5119pts/0 xx:xx:xxForever.SH 5128pts/0 xx:xx:xx Sleep 5129pts/0 xx:xx:xx PS[email protected] shellscript]$Kill 5118#向父shell发送TERM信号 [[email protected] shellscript]$PS-a #父shell仍未被杀掉 PID TTY time CMD3822pts/0 xx:xx:xx su 3830pts/0 xx:xx:xxBash3868pts/0 xx:xx:xx su 3869pts/0 xx:xx:xxBash4037pts/0 xx:xx:xxVim4038pts/0 xx:xx:xxVim4418pts/0 xx:xx:xxTraploop.SH 4499pts/0 xx:xx:xx Sleep 5118pts/0 xx:xx:xxSubsig.SH 5119pts/0 xx:xx:xxForever.SH 5133pts/0 xx:xx:xx Sleep 5134pts/0 xx:xx:xx PS[email protected] shellscript]$Kill 5119#向子shell发送TERM信号 [[email protected] shellscript]$ Terminated #子shell立刻被终止You want toKillme!#并打印出父shell对TERM信号的相应信息 [1]+ Exit143./subsig.SH#父shell随着子shell的终止而终止 [[email protected] shellscript]$
Linux Shell Signal Inheritance