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

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

How to kill a process with 'kill' combined with 'grep'

bashprocessgrepkillps

提问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 pgrepwhich you can use to replace your ps | greppipeline:

请注意,pgrep您还可以使用它来替换ps | grep管道:

pgrep myscriptname

It prints the PID for you, and nothing else.

它会为您打印 PID,仅此而已。

回答by Mark

Another alternative is using the pidofcommand:

另一种选择是使用pidof命令:

kill $(pidof processname)

回答by Oswaldo Ferreira

Another alternative, pgrepwith xargs

另一种选择,pgrepxargs

ps aux | pgrep gitlab | xargs kill

ps辅助| pgrep gitlab | xargs 杀死

回答by runningviolent

An alternative is piping to the xargscommand:

另一种方法是管道到xargs命令:

ps -ef | grep myscriptname | xargs kill

ps -ef | grep myscriptname | xargs kill

http://man7.org/linux/man-pages/man1/xargs.1.html

http://man7.org/linux/man-pages/man1/xargs.1.html