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- -Xmx32m -Djava.util.logging.config.file=config/ -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- -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 -Xmx256m -Djava.util.logging.config.file=config/ 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- -Xmx16m -Djava.util.logging.config.file=config/ -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- -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 -Xmx128m -Djava.util.logging.config.file=config/ 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:

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.

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.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

4 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 *