How to kill multiple processes at once with grep

Last updated on November 18, 2020 by Dan Nanni

There are cases where you want to kill multiple processes that match a certain pattern in their command line strings. For example, suppose you want to kill all processes that are running commands with keyword jppf in their arguments.

$ ps aux | grep jppf
xmodulo   3324  0.1  2.9 156524 15176 pts/0    Sl   22:16   0:01 /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/bin/java -Xmx32m -Djppf.config=jppf-driver.properties -Dlog4j.configuration=log4j-driver.properties -Djava.util.logging.config.file=config/logging-driver.properties -classpath /home/xmodulo/JPPF-3.2.2-driver/config:/home/xmodulo/JPPF-3.2.2-driver/lib/jmxremote_optional.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/jppf-common-node.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/jppf-common.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/jppf-server.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/log4j-1.2.15.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/slf4j-api-1.6.1.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/slf4j-log4j12-1.6.1.jar org.jppf.server.DriverLauncher
xmodulo   3338  0.3  4.4 391484 22792 pts/0    Sl   22:16   0:03 /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/bin/java -cp /home/xmodulo/JPPF-3.2.2-driver/config:/home/xmodulo/JPPF-3.2.2-driver/lib/jmxremote_optional.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/jppf-common-node.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/jppf-common.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/jppf-server.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/log4j-1.2.15.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/slf4j-api-1.6.1.jar:/home/xmodulo/JPPF-3.2.2-driver/lib/slf4j-log4j12-1.6.1.jar -Djppf.config=jppf-driver.properties -Dlog4j.configuration=log4j-driver.properties -Xmx256m -Djava.util.logging.config.file=config/logging-driver.properties org.jppf.server.JPPFDriver 59216
xmodulo   3388  0.1  2.9 139144 15064 pts/0    Sl   22:19   0:01 /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/bin/java -Xmx16m -Dlog4j.configuration=log4j-node.properties -Djppf.config=jppf-node.properties -Djava.util.logging.config.file=config/logging-node.properties -classpath /home/xmodulo/JPPF-3.2.2-node/config:/home/xmodulo/JPPF-3.2.2-node/lib/jmxremote_optional.jar:/home/xmodulo/JPPF-3.2.2-node/lib/jppf-common-node.jar:/home/xmodulo/JPPF-3.2.2-node/lib/log4j-1.2.15.jar:/home/xmodulo/JPPF-3.2.2-node/lib/slf4j-api-1.6.1.jar:/home/xmodulo/JPPF-3.2.2-node/lib/slf4j-log4j12-1.6.1.jar org.jppf.node.NodeLauncher
xmodulo   3402  0.1  3.9 257980 20288 pts/0    Sl   22:19   0:01 /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/bin/java -cp /home/xmodulo/JPPF-3.2.2-node/config:/home/xmodulo/JPPF-3.2.2-node/lib/jmxremote_optional.jar:/home/xmodulo/JPPF-3.2.2-node/lib/jppf-common-node.jar:/home/xmodulo/JPPF-3.2.2-node/lib/log4j-1.2.15.jar:/home/xmodulo/JPPF-3.2.2-node/lib/slf4j-api-1.6.1.jar:/home/xmodulo/JPPF-3.2.2-node/lib/slf4j-log4j12-1.6.1.jar -Djppf.config=jppf-node.properties -Dlog4j.configuration=log4j-node.properties -Xmx128m -Djava.util.logging.config.file=config/logging-driver.properties org.jppf.node.NodeRunner 49204

Here is a single command that will kill all processes at once, whose names contain <keyword>. Replace <keyword> with your own.

$ kill -9 `ps aux | grep <keyword> | grep -v grep | awk '{print $2}'`

The command line inside a pair of backtick characters (i.e., ps aux .... '{print $2}') will print out a list of process IDs that are matched with grep. The result is then used by the outer command kill. The grep -v grep is to exclude a self match (i.e., grep command itself) from a list of matched processes.

One caveat with this command is that when you are running it in a shell script, make sure to use bash, not sh.

If you are running the following script with sh:

#!/bin/sh
kill -9 `ps aux | grep jppf | grep -v grep | awk '{print $2}'`

You will get kill: Illegal number: error. The command line inside a pair of backticks is returning a multi-line response, and it appears that sh is not able to handle it. But bash can. So the following script should be okay.

#!/bin/bash
kill -9 `ps aux | grep jppf | grep -v grep | awk '{print $2}'`

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean