How to kill multiple processes at once with grep

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 that are matched with grep.

$ 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}'`

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

2 thoughts on “How to kill multiple processes at once with grep

  1. Have a look at pkill. It is much easier to use pkill than kill and grep. With pkill you can also find processes by other attributes, like the UID or the GID.

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *