Linux下进程杀不掉?"kill –9"来助阵

我的测试机中有个进程,使用一般的kill命令杀不掉,如下:

[root]tmelinux02# ps aux | grep mongoose                                                                                                                                                           /root
root     13950  0.0  0.0 112712   964 pts/0    S+   03:56   0:00 grep --color=auto mongoose
root     25872 42.0  3.0 48474464 9917640 ?    SNl  May18 9691:38 /opt/mongoose/jdk-11.0.5+10-jre/bin/java -jar /opt/mongoose/mongoose-bundle-4.2.15.jar --run-node
[root]tmelinux02# kill 25872                                                                                                                                                                       /root
[root]tmelinux02# ps aux | grep mongoose                                                                                                                                                           /root
root     13989  0.0  0.0 112712   968 pts/0    S+   03:57   0:00 grep --color=auto mongoose
root     25872 42.0  3.0 48474464 9917852 ?    SNl  May18 9691:39 /opt/mongoose/jdk-11.0.5+10-jre/bin/java -jar /opt/mongoose/mongoose-bundle-4.2.15.jar --run-node


于是使用了“kill -9”这一招,成功杀掉了进程。

[root]tmelinux02# kill -9 25872                                                                                                                                                                    /root
[root]tmelinux02# ps aux | grep mongoose                                                                                                                                                           /root
root     14166  0.0  0.0 112712   964 pts/0    S+   03:58   0:00 grep --color=auto mongoose


那么kill –9 跟 普通的kill有什么不同呢?

Kill命令会发送一个信号(signal)给指定的进程或一批进程。如果不指定任何信号(signal),那么默认就会发送TERM这个信号。如果信号进程接不到,比如已经hang住了,那么可以使用9这个信号来强制杀掉进程。


几个有用的信号的英文原版的解释:

  • SIGINT - This signal is the same as pressing ctrl-c. On some systems, "delete" + "break" sends the same signal to the process. The process is interrupted and stopped. However, the process can ignore this signal.
  • SIGTERM - This signal requests a process to stop running. This signal can be ignored. The process is given time to gracefully shutdown. When a program gracefully shuts down, that means it is given time to save its progress and release resources. In other words, it is not forced to stop. SIGINT is very similar to SIGTERM.
  • SIGKILL - The SIGKILL signal forces the process to stop executing immediately. The program cannot ignore this signal. This process does not get to clean-up either.
  • SIGSTOP - This signal makes the operating system pause a process's execution. The process cannot ignore the signal.
  • SIGCONT - To make processes continue executing after being paused by the SIGTSTP or SIGSTOP signal, send the SIGCONT signal to the paused process. This is the CONTinue SIGnal. This signal is beneficial to Unix job control (executing background tasks).


注意,下面这三个命令作用是一样的。

kill -9 1234
kill -KILL 1234
kill -SIGKILL 1234


值得注意的是kill –9, 即SIGKILL,不是发给进程的,而是发给操作系统的。当你指定了kill –9的时候,你并不是告诉应用程序让它自己停止下来,而是告诉操作系统干掉这个应用程序,甭管它现在正在干啥,杀就完了。这就是我们为什么我们开头杀不掉的进程,用kill –9 却可以杀掉的原因。


另一个发给操作系统的信号是SIGSTOP,只不过,SIGSTOP不会杀掉进程,而是冻结住进程的执行,你可以使用SIGCONT来在稍后对这个进程进行恢复。而这一切应用程序完全不知情。


参考资料

================

What is the purpose of the -9 option in the kill command?

https://askubuntu.com/questions/184071/what-is-the-purpose-of-the-9-option-in-the-kill-command#:~:text=When%20you%20run%20kill%20%2D9,program%20will%20immediately%20be%20stopped.

Kill Commands and Signals

https://www.linux.org/threads/kill-commands-and-signals.8881/

原文地址:https://www.cnblogs.com/awpatp/p/13038489.html