bash linux shell 终止信号 SIGKILL && KILL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16423732/
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 05:22:50  来源:igfitidea点击:

linux shell kill signal SIGKILL && KILL

linuxbashshellkill

提问by Red Lv

I just written a shell script to control the start and stop of a module. Everything seems normal until I find the stop command result in something unexpected.

我刚刚写了一个shell脚本来控制一个模块的启动和停止。一切似乎都很正常,直到我发现停止命令导致了一些意想不到的事情。

I use the command kill -s SIGKILL -- -gpidto kill a group of processes. I use the /bin/shto run the command like this

我使用该命令kill -s SIGKILL -- -gpid杀死一组进程。我使用/bin/sh来运行这样的命令

/bin/sh -c "kill -s SIGKILL -- -gpid"

which replied the error

哪个回答了错误

/bin/sh: line 0: kill: SIGKILL: invalid signal specification

Then I replaced the /bin/shwith /bin/bash, so the command is

然后我更换了/bin/sh/bin/bash,所以命令

/bin/bash -c "kill -s SIGKILL -- -gpid"

which replied nothing error. so I conclude the explanation that the difference between bash and sh cause the result. However, when I lsthe /bin/sh, I found the /bin/shis a symbolic link to /bin/bash, so the command should be the same.

它没有回答任何错误。所以我总结了 bash 和 sh 之间的差异导致结果的解释。然而,当我ls/bin/sh,我发现/bin/sh是一个符号链接/bin/bash,那么该命令应该是相同的。

I found the command syntax kill -s SIGKILLis not in the syntax recommended, kill -s KILLrecommended.

我发现命令语法kill -s SIGKILL不在推荐、kill -s KILL推荐的语法中。

so I replaced the SIGKILL with KILL, the command is

所以我用 KILL 替换了 SIGKILL,命令是

/bin/sh -c "kill -s KILL -- -gpid"

which replied nothing error. as described above, anyone could explained this case.

它没有回答任何错误。如上所述,任何人都可以解释这种情况。

回答by Kevin

When bash is invoked as sh(e.g. via symlink, as in your case), it uses a shcompatibility mode where most modern features are turned off. I'd bet shis calling the external binary for kill, and it doesn't recognize SIGKILL, but the bash invocation is using its builtin, and that builtin does.

当 bash 被调用时sh(例如通过符号链接,就像你的情况一样),它使用一种sh兼容模式,其中大多数现代功能都被关闭。我敢打赌,它sh正在为 调用外部二进制文件,但kill它无法识别SIGKILL,但 bash 调用正在使用其内置函数,而该内置函数确实如此。

回答by zwol

The only trulyportable way to write this command is

编写此命令的唯一真正可移植的方法是

kill -9 -$gpid

Noneof the ways to specify a signal namerather than a signal number work on the Unixes that froze their shell utilities in the mid-90s, which is basically all of them except Linux and the open-source BSDs. However, SIGKILL is reliably signal number 9 and has always been so (since V7 if not earlier).

的方式来指定一个信号名称,而不是在Unix系统是冻结其壳公用事业90年代中期,这基本上是所有的人,除了Linux和开源的BSD系统的信号数工作。然而,SIGKILL 是可靠的第 9 号信号并且一直如此(从 V7 开始,如果不是更早的话)。

The special argument --isn't portable either, and is unnecessary in this case.

特殊参数--也不是可移植的,在这种情况下是不必要的。

If you want to be a little more polite about it (sending SIGTERM instead) then use

如果您想对此更有礼貌(改为发送 SIGTERM),请使用

kill -15 -$gpid

Again, that number is reliable all the way back to V7.

同样,这个数字一直可靠到 V7。

回答by tobe

It's all about bash compatibility. Quick fix to use /bin/bashbecause shcan't recognize SIGINT or other features.

这都是关于bash 兼容性的。使用快速修复,/bin/bash因为sh无法识别 SIGINT 或其他功能。