bash 如何使用“kill”结合“grep”杀死进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36771670/
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 a process with 'kill' combined with 'grep'
提问by Edward
I'd like to kill a process/script with a simple command using. At the moment I do the following
我想用一个简单的命令杀死一个进程/脚本。目前我执行以下操作
ps -ef | grep myscriptname
kill 123456
But is there a way to maybe combine the 2 command together so I don't need to look and manually write the pid, something like this kill grep myscriptname
?
但是有没有办法将 2 个命令组合在一起,这样我就不需要查看和手动编写 pid,像这样kill grep myscriptname
?
回答by John Zwinck
You want pkill
:
你想要pkill
:
pkill myscriptname
On some systems there is a similar tool called killall
, but be careful because on Solaris it really does kill everything!
在某些系统上有一个类似的工具叫做killall
,但要小心,因为在 Solaris 上它确实会杀死一切!
Note that there is also pgrep
which you can use to replace your ps | grep
pipeline:
请注意,pgrep
您还可以使用它来替换ps | grep
管道:
pgrep myscriptname
It prints the PID for you, and nothing else.
它会为您打印 PID,仅此而已。
回答by Mark
Another alternative is using the pidof
command:
另一种选择是使用pidof
命令:
kill $(pidof processname)
回答by Oswaldo Ferreira
Another alternative, pgrep
with xargs
另一种选择,pgrep
与xargs
ps aux | pgrep gitlab | xargs kill
ps辅助| pgrep gitlab | xargs 杀死
回答by runningviolent
An alternative is piping to the xargs
command:
另一种方法是管道到xargs
命令:
ps -ef | grep myscriptname | xargs kill
ps -ef | grep myscriptname | xargs kill