为什么我在 bash 中的 kill -9 命令上没有收到信号 SIGKILL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22807714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why I am not getting signal SIGKILL on kill -9 command in bash?
提问by Jayesh Bhoi
In bash script I handle different signal as follows:
在 bash 脚本中,我处理不同的信号如下:
#!/bin/bash
sighdl ()
{
echo "signal caught"
#do something
exit 0
}
trap sighdl SIGKILL SIGINT SIGTERM
Above code handle signal properly for following activity:
以上代码正确处理以下活动的信号:
- Ctrl+C
kill pid
pkill scriptname
- Ctrl+C
kill pid
pkill scriptname
For kill -9 pid
it does not call sighdl
. As per my understanding (if I am not wrong) kill -9
sends the SIGKILL signal.
因为kill -9 pid
它不调用sighdl
。根据我的理解(如果我没记错的话)kill -9
发送 SIGKILL 信号。
Any idea?
任何的想法?
回答by anubhava
You cannot do that. Yes 9 is SIGKILL
and Unix system by design doesn't allow any script/program to trap SIGKILL
due to security reasons. Otherwise any script can trap & ignore SIGKILL which will make impossible to terminate that script by OS.
你不能这样做。是的 9 是SIGKILL
Unix 系统的设计不允许任何脚本/程序SIGKILL
由于安全原因而陷入困境。否则,任何脚本都可以捕获并忽略 SIGKILL,这将使操作系统无法终止该脚本。