How to kill all processes with a given partial name?

If you need more flexibility in selecting the processes use

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

You can use grep -e etc.


Kill all processes matching the string "myProcessName":

ps -ef | grep 'myProcessName' | grep -v grep | awk '{print $2}' | 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

Why "ps pipe kill" from terminal is evil:

The Piping of integers you scraped from ps -ef to kill -9 is bad, and you should feel bad, doubly so if you're root or a user with elevated privileges, because it doesn't give your process a chance to cleanly shut down socket connections, clean up temp files, inform its children that it is going away or reset its terminal characteristics.

Instead send 15, and wait a second or two, and if that doesn't work, send 2, and if that doesn't work, send 1. If that doesn't, REMOVE THE BINARY because the program is badly behaved.

As a general principle we don't use Unix Railgun to trim the hedges. https://porkmail.org/era/unix/award.html#kill

Explanation of above command:

ps -ef produces 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 grep says 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.

Why ps pipe kill is bad, dangerous, ugly and hackish:

  1. There's a small possibility that you will accidentally end the operating system or cause undefined behavior in an unrelated process, leading to whole system instability because ps -ef lists thousands of processes, and you can't be sure some 3rd party process 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 ended some random necessary process unrelated to yours.

  2. 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 kill part with a transmitted flag that requests a graceful shutdown, and only if that request is denied, last-resort to kill -9

But, if you understand all the risks and control for them with unique names, and you're ok with a few dropped transactions or occasional corruption, 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. "A Rogue Robot scraped ps to find integers and sent those to kill -9, so reboot the computer to clear the problem.

Why not just use pkill which is easier?

The above gives me manual control because ps, grep, awk, kill and xargs are multi-platform standard. It gives full control to which regex engine to use, which part of the process name to match, handling case sensitivity and exception management.

pkill -f -e -c myProcessName

Does the same thing for me, but see man pkill has different behaviors, flags and regex engines between variants of Linux, Mac, Zune-Bash and my opensource router. So yes, put your 35000 Watt Unix-Railgun into the capable hands of pkill to trim the hedges. See what happens.


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

pkill -f my_pattern

Just in case it doesn't work, try to use this one as well:

pkill -9 -f my_pattern

Tags:

Linux

Bash

Posix