Linux: kill matching processes

If you need to kill all processes matching a given pattern, you can use the commands described here.

First you need to get the list of all processes. This can be done with any of the following commands:

# ps -A
# ps -e
# ps ax

But since we want to kill based on the command line used to start the process, we need to also get it in the listing. ps ax already returns it. But ps -e and ps -A not unless you use the -F parameter. So here are the commands you should use to see the process information required to filter the processes to kill:

# ps -AF
# ps -eF
# ps ax

Now we can use grep to filter the processes we want to kill. You have to use two greps:

  • One grep to filter the processes
  • One grep to filter out the first grep

So:

# ps -AF | grep http | grep -v grep
# ps ax | grep http | grep -v grep
# ps -eF | grep http | grep -v grep

Then you need to extract the process IDs. This is the first column for ps ax and the second column for the other ones:

# ps -AF | grep http | grep -v grep | awk ' { print $2; }'
# ps -eF | grep http | grep -v grep | awk ' { print $2; }'
# ps ax | grep http | grep -v grep | awk ' { print $1; }'

The kill command can get a list of process IDs as parameters, so we need to convert the vertical list returned by the previous commands to an horizontal one using xargs and call kill:

# ps -AF | grep http | grep -v grep | awk ' { print $2; } | xargs kill'
# ps -eF | grep http | grep -v grep | awk ' { print $2; } | xargs kill'
# ps ax | grep http | grep -v grep | awk ' { print $1; } | xargs kill'

If you want to force a kill, you should additionally use the -9 option:

# ps -AF | grep http | grep -v grep | awk ' { print $2; } | xargs kill -9'
# ps -eF | grep http | grep -v grep | awk ' { print $2; } | xargs kill -9'
# ps ax | grep http | grep -v grep | awk ' { print $1; } | xargs kill -9'

Update: As BFU rightly pointed out, you can also use the killall command to kill all processes started with a given command:

# killall java

This will kill all java processes. But it has two drawbacks:

  1. You need to know the exact name of the process you want to kill.
  2. You cannot kill processes started with the same program name based on their arguments e.g. do not kill all java processes but only those using a given JAR file

Since we’re talking about alternatives, you can also use the following (which is of course much less fun…):

# pkill -f apache2

Without the -f option, pkill will only match the program name. With -f it will match the whole command line.

2 thoughts on “Linux: kill matching processes

    1. killall is a little bit different. You can’t see it from my post but I mostly use this to kill some java processes and filter not based on the command which is java for all of them but on the arguments used to start it. But I’ll add an update to the post that killall is easier way when you want to kill all instances of a command.

Leave a Reply

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