Skip to content

Bash snippet: Process Search & Destroy

2011 April 12
by Tedb0t

Occasionally on my server I’ll get dead processes that fit a specific command pattern, and I need to kill them.  However, I don’t want to killall, as other processes of the same program are still running normally.  Here’s a handy one-liner I just whipped up to do this:

sudo kill `ps ax -o"pid,command" | grep -E 'command_pattern' | awk '{print $1}'`
ps ax -o"pid,command"

gives you a list of all processes, just showing pid and command.

grep -E 'command_pattern'

searches for a regex command pattern.

awk '{print $1}'

returns the first column—the pid. This is the ultimate output of the backticked expression, which is substituted as the parameter for kill.

Hope you find it useful!

Related Posts:

Comments are closed.