Linux 如何杀死具有给定部分名称的所有进程?

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

How to kill all processes with a given partial name?

linuxbashposix

提问by ?ukasz Lew

I want to kill all processes that I get by:

我想杀死我得到的所有进程:

ps aux | grep my_pattern

How to do it?

怎么做?

This does not work:

这不起作用:

pkill my_pattern

采纳答案by Dor Shemer

Use pkill -f, which matches the pattern for any part of the command line

Use pkill -f,它匹配命令行任何部分的模式

pkill -f my_pattern

回答by Eugen Rieck

If you need more flexibility in selecting the processes use

如果您需要更灵活地选择流程,请使用

for KILLPID in `ps ax | grep 'my_pattern' | awk ' { print ;}'`; do 
  kill -9 $KILLPID;
done

You can use grep -e etc.

您可以使用 grep -e 等。

回答by Eric Leschinski

Kill all processes matching the string "myProcessName":

杀死与字符串“myProcessName”匹配的所有进程:

ps -ef | grep 'myProcessName' | grep -v grep | awk '{print }' | xargs -r kill -9

Source: http://www.commandlinefu.com/commands/view/1138/ps-ef-grep-process-grep-v-grep-awk-print-2-xargs-kill-9

来源:http: //www.commandlinefu.com/commands/view/1138/ps-ef-grep-process-grep-v-grep-awk-print-2-xargs-kill-9

What's this code doing?

这段代码是干什么的?

The ps -efproduces a list of process id's on the computer visible to this user. The pipe grep filters that down for rows containing that string. The grep -v grepsays don't match on the process itself doing the grepping. The pipe awk print says split the rows on default delimiter whitespace and filter to the second column which is our process id. The pipe xargs spins up a new process to send all those pid's to kill -9, ending them all.

ps -ef计算机给该用户可见的生产的进程ID的列表。管道 grep 过滤掉包含该字符串的行。该grep -v grep说,不匹配的过程本身做grepping。管道 awk 打印表示在默认分隔符空白处拆分行并过滤到第二列,即我们的进程 ID。管道 xargs 启动一个新进程将所有这些 pid 发送到kill -9,结束它们。

The above code is bad, dangerous, ugly and hackish for several reasons.

出于几个原因,上面的代码是糟糕的、危险的、丑陋的和骇人听闻的。

  1. If the code being force-ended is doing any database ops or secure transactions with low probability race conditions, some fraction of a percent of the time, atomicity of that transaction will be wrecked, producing undefined behavior. kill -9 takes no prisoners. If your code is sensitive to this, try replacing the xargs killpart with a transmitted flag that requests a graceful shutdown, and only if that request is denied, last-resort to kill -9

  2. There's a non zero possibility that you will accidentally end the operating system or caused undefined behavior in an unrelated process, leading to whole system instability because ps -eflists every possible process that could exist, and you can't be sure some weird 3rd party library shares your process name, or that in the time between read and execute kill -9, the processid had changed to something else, and now you've accidentally ended some random process you didn't intend to.

  1. 如果被强制终止的代码正在执行任何数据库操作或具有低概率竞争条件的安全事务,那么在百分之几的时间内,该事务的原子性将被破坏,产生未定义的行为。kill -9 没有俘虏。如果您的代码对此敏感,请尝试xargs kill使用请求正常关闭的传输标志替换该部分,并且仅当该请求被拒绝时,最后的手段kill -9

  2. 有一个非零的可能性,你会意外地结束操作系统或在不相关的进程中导致未定义的行为,导致整个系统不稳定,因为ps -ef列出了可能存在的每个可能的进程,并且你不能确定一些奇怪的 3rd 方库共享你的进程名称,或者在读取和执行 kill -9 之间的时间,进程 ID 已更改为其他内容,现在您不小心结束了一些您不打算执行的随机进程。

But,if you understand the risks and control for them with very unique names, and you're ok with a few dropped transactions or occasional corruption in data, then 99.9% of the time yer gonna be fine. If there's a problem, reboot the computer, make sure there aren't any process collisions. It's because of code like this that makes the tech support script: "Have you tried restarting your computer" a level 5 meme.

但是,如果您使用非常独特的名称了解风险和控制,并且您可以接受一些丢失的交易或偶尔的数据损坏,那么 99.9% 的时间您都会没事的。如果出现问题,请重新启动计算机,确保没有任何进程冲突。正是因为这样的代码使得技术支持脚本:“你试过重新启动你的计算机吗”成为 5 级模因。

回答by Nived Thanima

you can use the following command to list the process

您可以使用以下命令列出进程

ps aux | grep -c myProcessName 

if you need to check the count of that process then run

如果您需要检查该进程的计数然后运行

ps aux | grep -c myProcessName |grep -v grep 

after which you can kill the process using

之后你可以使用杀死进程

kill -9 $(ps aux | grep -e myProcessName | awk '{ print  }') 

回答by user6482587

Sounds bad?

听起来很糟糕?

 pkill `pidof myprocess`

example:

例子:

# kill all java processes
pkill `pidof java`

回答by kraymer

If you judge pkill -f PATTERNa bit too dangerous, I wrote ezkilla bash script that prompt you to choose which processes amongst those that match the PATTERN you want to kill.

如果你觉得pkill -f PATTERN有点太危险了,我写了ezkill一个 bash 脚本,提示你在那些与你想要杀死的 PATTERN 匹配的进程中选择哪些进程。

回答by Luis Lopes

You can use the following command to:

您可以使用以下命令:

ps -ef | grep -i myprocess | awk {'print '} | xargs kill -9

or

或者

ps -aux | grep -i myprocess | awk {'print '} | xargs kill -9

It works for me.

这个对我有用。

回答by Alex Liu

You can use the following command to

您可以使用以下命令

kill -9 $(ps aux | grep 'process' | grep -v 'grep' | awk '{print }')

回答by Alex

Also you can use killall -r my_pattern. -rInterpret process name pattern as an extended regular expression.

你也可以使用killall -r my_pattern. -r将进程名称模式解释为扩展的正则表达式。

killall -r my_pattern

回答by ajaz

If you do not want to take headache of finding process id, use regexp to kill process by name. For example, to kill chrome following code will do the trick.

如果您不想为查找进程 ID 而头疼,请使用正则表达式按名称杀死进程。例如,杀死 chrome 以下代码就可以解决问题。

killall -r --regexp chrome

killall -r --regexp chrome