Killmatching: a violent killall.

I always seem to need a better killall especially when running script or Java processes where the command name is, for example, "java". Scratching the itch, here is killmatching

#!/bin/bash -e

# usage: killmatching [ kill-options] pattern1 pattern2 ... patternN

PS_OPTS=${PS_OPTS:- -A -o pid,command}

while [ 0 -ne $(expr $1 : '\-') ]
do
KILL_OPTS="$KILL_OPTS $1"
shift 1
done

for pattern in "$@"
do
for pid in $(ps $PS_OPTS \
| grep -v -F grep \
| grep -v -F "$0" \
| grep -e "$pattern" \
| awk '{ print $1 }')
do
kill $KILL_OPTS $pid
done
done

Be very careful using it with one wrong pattern and you can shutdown the computer.

Some ps commands do not support the -A and -o options. Edit these options for your ps.