<--Directory--
1) Trap Introduction
2) Trap Signal list
3) Trap Use example
4) test the INT (2) signal
5) Simultaneous testing of multiple signals
6) Shell Springboard machine case
"Trap Introduction"
The trap command is used to specify the action to take after the signal is received; a common use of the trap command is to complete the cleanup when the script is interrupted, and the shell always uses numbers to represent the signal.
"Trap Signal List"
Some of the more important signals are listed below (the numbers in parentheses are the traditional signal numbers)
Signal Description
HUP (1) hangs, usually caused by terminal drop or user exit
INT (2) interrupts, usually caused by pressing the CRTL+C combination
Quit (3) exit, usually caused by some serious execution error
ABRT (6) Abort, usually caused by some serious execution error
ALRM (14) Alarm, typically used to handle Timeouts |
Term (15) termination, usually sent at system shutdown
TSTP (20) stops the operation of the process, but the signal can be processed and ignored, and the user susp the character (usually ctrl-z) to emit this signal.
"Trap Use Example"
Usually we need to ignore the signal that has hup INT QUIT TSTP term, which is the signal 1,2,3,20,15 use the following statement to make these interrupt signals ignored
The parameters of the trap command are divided into two parts, the first part is the action to be taken when the specified signal is received, and the latter part is the signal name to be processed, as in the following example:
########################
# Trap Command Signal #
########################
Signal refers to the received signal, command refers to the action taken to receive the signal, as follows two simple signals
######################################################################################################
# trap "Command (Action command to execute)" 1 2 3 20 15 or trap "command (Action command to execute)" HUP INT QUIT TSTP #
######################################################################################################
"Test int (2) signal"
[[Email Protected]lhost ~]# Trap "] 2 <== shielded CTRL + C signal," "" "inside the quotation mark is empty, that is, you press the CRTL+C, do not do any action, the equivalent of shielding the CTRL + C
[[email protected] ~]# cannot execute CTRL + C at this time
[[email protected] ~]# trap ":" 2 <== Restore CTRL + C signal
[[email protected] ~]# ^c <== can execute crtl+c now
[[email protected] ~]# trap "Echo-n ' is typing CTRL + C '" 2 <== Press CTRL + C after receiving 2 signal will output double quotation marks inside the content
[Email protected] ~]# ^cyou is typing CTRL + C
[Email protected] ~]# ^cyou is typing CTRL + C
[Email protected] ~]# ^cyou is typing CTRL + C
[[email protected] ~]# trap ":" 2 <== and then restore the sample CTRL + C signal
[[email protected] ~]# ^c <== then press Crtl+c to change to the original output
"Test multiple signals simultaneously"
[[email protected] ~]# trap "" HUP INT QUIT TSTP term
[[email protected] ~]# trap ":" HUP INT QUIT TSTP term
[[email protected] ~]# trap "" 1 2 3 20 15
[[email protected] ~]# trap ":" 1 2 3 20 15
"Shell Springboard case" (shielding signal after triggering signal)
Method:
1) first do Ssh+key certification
2) Implement the traditional Remote Connection menu selection script
3) Use of Linux signals to prevent users from operating on a springboard machine
4) When the user logs in, the script is called.
[Email protected] ~]# cat/etc/profile.d/tiaoban.sh
[$UID-ne 0] &&. /server/scripts/tiaoban.sh
[Email protected] ~]# cat/server/scripts/tiaoban.sh
function trapper () {
Trap ': ' INT EXIT tstp TEMP HUP
}
While:
Do
Trapper #我在这里调用函数trapper, but called the above five signals, which have a central CTRL + C signal
Clear #清屏
Cat <<menu #终端打印出下面三排菜单供普通用户选择
1) Web A
2) Web B
3) exit
Menu
Read-p "Please select:" Num #让用户选择一个数字
Case ' $num ' in
1)
Echo 1
SSH 192.168.1.107
;;
2)
Echo 2
SSH 192.168.1.108
;;
3|*)
Exit
Esac
Done
Normal User Test Login:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/82/EB/wKiom1dlAQjzY7wDAAB2roUmxwg461.png-wh_500x0-wm_3 -wmp_4-s_575981912.png "title=" qq picture 20160618160719.png "alt=" Wkiom1dlaqjzy7wdaab2roumxwg461.png-wh_50 "/>
Enter a normal user name and password
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/82/EA/wKioL1dlAUfyr8VrAABPM3KvaT8814.png-wh_500x0-wm_3 -wmp_4-s_2160818615.png "title=" qq picture 20160618160719.png "alt=" Wkiol1dlaufyr8vraabpm3kvat8814.png-wh_50 "/>
Here to press CTRL + C no effect, you must choose to tap a digital connection, or choose to knock 3 to exit
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/82/EB/wKiom1dlAyixA6pIAAAYXRilLsQ103.png "title=" QQ picture 20160618160719.png "alt=" Wkiom1dlayixa6piaaayxrillsq103.png "/>
This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1790623
Trap Signal Capture Detailed