为什么我在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:05:14  来源:igfitidea点击:

Why I am not getting signal SIGKILL on kill -9 command in bash?

linuxbashsignals

提问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:

以上代码正确处理以下活动的信号:

  1. Ctrl+C
  2. kill pid
  3. pkill scriptname
  1. Ctrl+C
  2. kill pid
  3. pkill scriptname

For kill -9 pidit does not call sighdl. As per my understanding (if I am not wrong) kill -9sends the SIGKILL signal.

因为kill -9 pid它不调用sighdl。根据我的理解(如果我没记错的话)kill -9发送 SIGKILL 信号。

Any idea?

任何的想法?

回答by anubhava

You cannot do that. Yes 9 is SIGKILLand Unix system by design doesn't allow any script/program to trap SIGKILLdue to security reasons. Otherwise any script can trap & ignore SIGKILL which will make impossible to terminate that script by OS.

你不能这样做。是的 9 是SIGKILLUnix 系统的设计不允许任何脚本/程序SIGKILL由于安全原因而陷入困境。否则,任何脚本都可以捕获并忽略 SIGKILL,这将使操作系统无法终止该脚本。