Signal is a process communication mechanism in Linux system. We can use a specific signal to interrupt the process. Each signal is associated with an integer value.
The KILL command can be used to send a signal to a process, while the trap command handles the received signal.
Kill
List all available signals
[email protected] ~]# kill-L1) SIGHUP2) SIGINT3) Sigquit4) Sigill5) SIGTRAP6) SIGABRT7) Sigbus8) SIGFPE9) SIGKILLTen) SIGUSR1 One) SIGSEGV A) SIGUSR2 -) Sigpipe -) SIGALRM the) SIGTERM -) Sigstkflt -) SIGCHLD -) Sigcont +) SIGSTOP -) SIGTSTP +) Sigttin A) Sigttou at) Sigurg -) sigxcpu -) Sigxfsz -) SIGVTALRM -) sigprof -) Sigwinch in) SIGIO -) SIGPWR to) Sigsys the) sigrtmin *) sigrtmin+1 $) sigrtmin+2 Panax Notoginseng) sigrtmin+3 -) sigrtmin+4 the) sigrtmin+5 +) sigrtmin+6 A) sigrtmin+7 the) sigrtmin+8 +) sigrtmin+9 -) sigrtmin+Ten $) sigrtmin+ One $) sigrtmin+ A -) sigrtmin+ - -) sigrtmin+ - the) sigrtmin+ the -) sigrtmax- - Wuyi) sigrtmax- - the) sigrtmax- A -) sigrtmax- One Wu) sigrtmax-Ten -) sigrtmax-9 About) sigrtmax-8 $) sigrtmax-7 -) sigrtmax-6 -) sigrtmax-5 -) sigrtmax-4 A) sigrtmax-3 +) sigrtmax-2 the) sigrtmax-1 -) Sigrtmax
Only a few of them are often used.
SIGHUP 1 -- suspend detection of the control process or end of the terminal.
SIGINT 2 -- Send this signal when CTRL + C
SIGKILL 9 -- strong kill process
SIGTERM 15-- default termination process
SIGTSTP 20--ctrl+z When the signal is sent
Usage, such as strong kill httpd process 19421
19421~]# Kill-919421
Trap
Once the trap assigns a signal to a function, the corresponding function is executed when the script receives the corresponding signal.
Example 1:
[email protected] sbin]# Vim demo_trap.sh #/bin/bashfunction hander () { is ' hander ' SIGINT while true; Do 1done
Ps: The function of the dead loop is to keep the process active.
Execute, and press CTRL + C
is 11756^chey,received signal:sigint
Example 2, the production environment is used as a springboard machine, so that normal operation users can only operate scripts
[email protected] profile.d]# pwd/etc/profile.d[[email protected] profile.d]# vim tiaoban.sh
function trapper () {
Trap ': ' INT EXIT TSTP term HUP
}
If [$UID-ne 0];then
While True;do
Trapper
Clear
echo [1] WEB Server
echo "" Echo [2] APP Server echo""Echo [3] DB Server echo""echo [x] Exit echo""Read-P"Option:"Nu Case$nuinch 1) SSH [email protected]192.168.1.20 ;; 2) SSH [email protected]192.168.1.30 ;; 3) SSH [email protected]192.168.1.40 ;; x) exit;; *) echo"usage:[123x]"Read-P"Press any key to continue."Esacdonefi
When logged in with a normal user, execute the script directly, and press CTRL + C to enter the command line
Shell Script Learning Summary 12--system signal