The Kill Command seems to mean "kill" at first glance, but in fact, the kill command is used to send signals. Let's take a look at the types of signals first.
Type of signal
Signal: a message sent by one process to another process.
There are more than a dozen signals in the Linux system, and each signal is assigned a number and a name. Common signals are as follows:
kill -l or trap -l # view all signal commands in the system
[root@Centos8 shell_scripy]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
We can see that there are many kinds of signals. We are most concerned about signal 9 and information number 15. 9 indicates unconditional termination and 15 indicates termination of the specified process;
kill Command
The kill command can send various signals to a process. Don't think it can only kill the process because of the name of kill.
Format of Kill Command: kill [- s sigspec | - n Signum | - sigapec] [PID | jobspec]
Note: specify the signal to be transmitted through sigspec or signum. Sigspec can be the name of the signal in any case (with or without SIG prefix) or the number assigned to the signal, and signum can only be a number. PID is the process number of the process. (use ps -fC process name or jobs -l to view the PID of the process)
The Kill Command will send the specified signal to the process with the process number pid or the process with the job number jobspec. If no signal is specified for the Kill Command, the Kill Command sends the SIGTERM signal by default, and the process receiving the SIGTERM command will end running. This is the process of killing the process with the Kill Command under normal circumstances. Remind again: don't think it can only kill the process because of the name of kill. In fact, the kill command can send various signals to a process.
Example: kill the background process with PID 4269:
① kill -n 15 4211 # specifies that the digital number of the signal is 15 and sends it to the process with PID 4211
② kill -s HUP 4269 or kill-s sighup 4269 or kill -n 1 4269 #3 are equivalent
③ kill 4215 # does not specify a signal, then the default SIGTERM signal is sent
Sometimes when we execute a script, many temporary files may be generated. Of course, we can write a command at the end of the script to delete these unnecessary temporary files, but when our script program is Ctrl+c or kill ed during execution, because the program may have generated temporary files but be terminated before executing the delete command, Therefore, the temporary file has not been deleted at this time. At this time, we can use the signal mechanism to automatically solve this problem. That is, you need to customize a callback function to perform specific operations when receiving these termination signals in the script.
Trap command syntax: trap commands signal1 signal2 signal3
Note: commands can be a series of commands or a function name, followed by the number or signal name of a series of signals. Any one of these signals is sent to the shell script, or when any one of these signals is captured by the script, the specified command or function will be executed.
In fact, the commands command of trap can be omitted. When the commands command is not specified, the shell script will execute their default values when receiving these signals, and each signal has a default value.
Note: when the shell script process receives a signal, it will pause the current work and check which signal it is, and then check whether the signal is ignored. If it is not ignored, execute the commands command. If there is no commands command, execute the default behavior of the signal. After executing the operations related to the commands command, if there is no exit command to exit the script, Then it returns to the place where it received the signal and continues to execute the next instruction. Therefore, if you need to exit when receiving the signal, you need to write an exit command in the commands command.
Although the signal 0 cannot be found in kill-l or trap -l, it exists when it is a pseudo signal corresponding to exit (whether there is a number after exit or not); In other words, the pseudo signal 0 will be triggered when the program is about to exit or exit at the end of the program execution. Using this feature, we can do some aftercare work after the script is executed. Such as trap show_goodbey 0.
Masking signal: when the process does not want to be interrupted by a signal in the middle of the process, we can specify that the trap operation is empty or: to mask the default operation of this signal (: is a built-in command of bash, which always indicates that it will not be done and returns 0, so it is more appropriate to represent an empty operation). For example, trap '' 1 15 or trap: 1 15
Note: according to personal understanding, the signal acquisition should be performed after searching upward from the place where the signal is received and finding the trap command corresponding to the corresponding signal. Therefore, if there are n trap commands for the same signal processing in front at this time, they will not be executed, and only the trap command of the first corresponding signal found by upward search will be executed, If there is a trap command with the same signal below the place where the signal is received, it will not be executed.
Note: it is recommended to use the - 15 signal, because in this way, the parent process and its child processes will release resources before terminating. However, the 9 information directly kills the process is too barbaric, which may cause the parent process to be killed and its child process to remain. In this way, the zombie process exists, and the zombie process will be hosted by process 1.
Demo example:
Kill process with 9 signal: [root@Centos8 ~]# ps -ef | grep nginx #View the process number PID of nginx root 2705 1 0 12:59 ? 00:00:00 nginx: master process nginx nginx 2706 2705 0 12:59 ? 00:00:00 nginx: worker process root 2708 1769 0 12:59 pts/0 00:00:00 grep --color=auto nginx [root@Centos8 ~]# kill -9 2705 [root@Centos8 ~]# ps -ef | grep nginx #It is found that there is a work process of nginx and a zombie process. At the same time, the work process will be hosted by process 1 because its parent process is dead nginx 2706 1 0 12:59 ? 00:00:00 nginx: worker process root 2721 1769 0 13:00 pts/0 00:00:00 grep --color=auto nginx [root@Centos8 ~]# kill -9 2706 #Continue to kill the work process [root@Centos8 ~]# ps -ef | grep nginx #In this way, all nginx processes have been killed root 2773 1769 0 13:03 pts/0 00:00:00 grep --color=auto nginx Kill the process with 15 signals: [root@Centos8 ~]# nginx [root@Centos8 ~]# ps -ef | grep nginx root 2864 1 0 13:10 ? 00:00:00 nginx: master process nginx nginx 2865 2864 0 13:10 ? 00:00:00 nginx: worker process root 2867 1769 0 13:10 pts/0 00:00:00 grep --color=auto nginx [root@Centos8 ~]# kill -15 2864 #Use the 15 signal to kill the master process, and all processes, including their child processes, are killed [root@Centos8 ~]# ps -ef | grep nginx root 2869 1769 0 13:10 pts/0 00:00:00 grep --color=auto nginx
Kill Command
If we already know the name of the process we want to kill and we can use the kill command to send the same signal, then all the parent processes and child processes of the process will receive a message, like this:
[root@Centos8 ~]# nginx [root@Centos8 ~]# ps -ef | grep nginx root 2897 1 0 13:13 ? 00:00:00 nginx: master process nginx nginx 2898 2897 0 13:13 ? 00:00:00 nginx: worker process root 2900 1769 0 13:13 pts/0 00:00:00 grep --color=auto nginx [root@Centos8 ~]# killall -15 nginx #Use kill to specify the process name directly [root@Centos8 ~]# ps -ef | grep nginx root 2903 1769 0 13:13 pts/0 00:00:00 grep --color=auto nginx