Skip to content

Instantly share code, notes, and snippets.

@alecthegeek
Created September 14, 2009 04:37
Show Gist options
  • Save alecthegeek/186500 to your computer and use it in GitHub Desktop.
Save alecthegeek/186500 to your computer and use it in GitHub Desktop.
Kill a proccess identifed vy a string
#! /bin/sh
# See http://partmaps.org/era/unix/award.html#uuk9letter
string="$1" ; export string
scriptname=`basename $0` ; export scriptname
kps=`ps -ef|nawk '$0 ~ ENVIRON["string"] && $0 !~ ENVIRON["scriptname"] {print $2}'`
echo $string has PID \"$kps\"
if test -z "$kps" ; then
echo "Cannot find process identified by $string"
exit 1
fi
for i in 15 2 1 ; do
kill -$i $kps
sleep 10
kps=`ps -ef|nawk '$0 ~ ENVIRON["string"] && $0 !~ ENVIRON["scriptname"] {print $2}'`
if test -z "$kps" ; then
echo Killed $string $kps with signal $i
exit 0
fi
done
echo "Unable to kill $string, PID = $kps"
exit 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment