如何在 Bash 中杀死进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30542550/
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
How to Kill Processes in Bash
提问by HymanWM
I used the following bash code:
我使用了以下 bash 代码:
for pid in `top -n 1 | awk '{if( == "R") print ;}'`
do
kill $pid
done
It says:
它说:
./kill.sh: line 3: kill: 29162: arguments must be process or job IDs
./kill.sh: line 3: kill: 29165: arguments must be process or job IDs
./kill.sh: line 3: kill: 29166: arguments must be process or job IDs
./kill.sh: line 3: kill: 29169: arguments must be process or job IDs
What causes this error and how do I kill processes in Bash?
导致此错误的原因是什么,如何在 Bash 中终止进程?
回答by Havenard
Probably this awk
command is not returning any reliable data, in any case theres a much easier way:
可能这个awk
命令没有返回任何可靠的数据,无论如何有一个更简单的方法:
kill `pidof R`
Or:
或者:
killall R
回答by pyrocrasty
It seems there may be a (possibly aliased) script kill.sh
in your current directory which is acting as an intermediary and calling the kill
builtin. However, the script is passing the wrong arguments to the builtin. (I can't give details without seeing the script.)
kill.sh
您当前的目录中似乎有一个(可能是别名的)脚本,它充当中介并调用kill
内置程序。但是,脚本将错误的参数传递给内置函数。(没有看到剧本,我无法提供细节。)
Solution.
解决方案。
Your command will work fine using the kill
builtin. The simplest solution is to ensure you use the bash builtin kill
. Execute:
您的命令可以使用kill
内置命令正常工作。最简单的解决方案是确保您使用内置的 bash kill
。执行:
chmod a-x kill.sh
unalias kill
unset -f kill
This will prevent the script from running and remove any alias or function that may interfere with your use of the kill
builtin.
这将阻止脚本运行并删除任何可能干扰您使用kill
内置函数的别名或函数。
Note.
笔记。
You can also simplify your command:
您还可以简化您的命令:
kill `top -n 1 | awk '{if( == "R") print ;}'`
Alternatively...
或者...
You can also use builtin
to bypass any functions and aliases:
您还可以使用builtin
绕过任何函数和别名:
builtin kill `top -n 1 | awk '{if( == "R") print ;}'`
回答by Alessandro Carini
Please try:
请尝试:
top -n 1 | awk '{print "(" ") - (" ")";}'
to understand how $1 and $8 are evaluated
了解 $1 和 $8 是如何计算的
Please also post the content of ./kill.sh and explain the purpose of this script
也请贴出./kill.sh的内容并说明这个脚本的用途
回答by Exeleration-G
I usuallly use:
我通常使用:
pkill <process name>
In your case:
在你的情况下:
pkill R
Note that this will kill all the running instances of R
, which may or may not be what you want.
请注意,这将杀死 的所有正在运行的实例R
,这可能是您想要的,也可能不是。
回答by ikegami
top
issues a header you need to suppress or skip over.
top
发出您需要抑制或跳过的标题。
Or maybe you can use ps
since its output is highly configurable.
或者你可以使用ps
它,因为它的输出是高度可配置的。
Also note that kill
accepts multiple PIDs, so there's no reason to employ a loop.
另请注意,它kill
接受多个 PID,因此没有理由使用循环。
kill $( ps h -o pid,state | awk ' == "R" { print }' )
回答by David C. Rankin
In addition to awk
, sed
can provide pid
isolation in the list for kill
:
此外awk
,sed
可以pid
在列表中为提供隔离kill
:
kill $(ps h -o pid,state | grep R | sed -e 's/\s.*$//')
Basically, the opposite side of the same coin.
基本上,同一枚硬币的反面。