linux kill命令-9 vs -15 [英] linux kill command -9 vs -15
问题描述
我有一个要杀死的进程,然后重新启动服务.有人编写了一段代码,通过编写以下脚本来终止该过程
I have a process that i would like to kill and then restart a service. Someone has written a code to kill the process by writing the following set of scripts
ps -ef |grep "process_name" | awk '{print "kill -15 " $2}'> /projects/test/kill.sh
#run the kill script
/projects/test/kill.sh
然后再次
ps -ef |grep "process_name" | awk '{print "kill -9 " $2}'> /projects/test/kill.sh
#run the kill script
/projects/test/kill.sh
#finally
service restart command here
# the problem here is that service does not restart properly sometimes,
as it thinks that process is still running.
据我了解,kill -15会优雅地终止进程.但是马上他们也有杀死-9的能力.所以如果一个进程在第一个命令中被杀死,当 kill -9 也在同一个进程上运行时会发生什么?还是ps -ef甚至会列出该进程,因为它已被标记为杀死?
As i understand kill -15 gracefully kills the process. But then right away they have the kill -9 as well. So if a process was getting killed in the first command, what happens when kill -9 is also run on the same process? Or will the ps -ef even list out that process since it has been marked for kill?
谢谢!
推荐答案
您正确地认为 kill -15
是优雅地杀死进程.但是,杀死一个进程是瞬间发生的.因此,上面的程序将检查 pid
,尝试优雅地将其杀死..如果 kill -15
失败-The > kill -9
被执行.知道 kill -15
失败的方法是 grep
命令.如果 kill -15
成功,则该 pid
应该不再存在,从而使以下 grep
返回空.
You are correct that kill -15
is to gracefully kill a process. But, killing a process is something that happens instantaneously. So the program above is going to check for pid
, attempting to kill it gracefully .. If the kill -15
fails -- The kill -9
is performed. The way it knows that kill -15
failed, is the grep
command. If kill -15
was successful, that pid
should not exist any longer, making the following grep
return empty.
实际上,仅当 kill -15
无法正常停止程序时, kill -9
才运行.这种方法的问题在于,有时根据程序的不同,正常停止进程可能需要一些时间.所以恕我直言,需要等待一段时间或 sleep
几秒钟,以允许 kill -15
尝试优雅地停止进程.. 最有把握使用上述方法,几乎总是调用 kill -9
,因为该脚本没有太多时间可以正常关闭该进程.如果 kill -15
仍在处理,kill -9
将覆盖并立即停止进程.
So really, kill -9
only runs if kill -15
failed to gracefully stop the program. The problem with this approach, is that sometimes gracefully stopping a process can take some time depending on the program. So IMHO there needs to be a wait period or a sleep
for a few seconds to allow kill -15
to attempt to gracefully stop the process .. Most assuredly with the approach above, kill -9
is almost always invoked since the script doesn't allow much time for the process to be shut down properly. In the event that kill -15
is still processing, kill -9
will just override and instantly stop the process.
这篇关于linux kill命令-9 vs -15的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!