Subject: Re: kill in bash script
To: Jan Muenther <jan@atstake.com>
From: David Laight <david@l8s.co.uk>
List: netbsd-help
Date: 11/13/2002 21:21:46
> 
> for i in `ps a | grep whateverprogram | awk '{print $1}'`; do kill
> -WHATEVERSIGNAL $i; done

Mmm.. this construct is all too common, especially in stop scripts
for unixware and solaris.  It has one major bug in that the
grep will pull out the string wherever it appears in the arguments.
People normally remember to exclude the grep, but not any other
command which happens to have 'whateverprogram' anywhere in its
argument list.

Putting 'word delimiters' (ie grep '\<whateberprogram\>') around
the pattern helps, but isn't really enough and is rarely done.

With netbsd's ps, you can do the following:

ps -ax -opid,command | while read pid command args
do
	[ "$command" = whateverprogram ] && kill $pid
done

which is relatively unlikely to kill the wrong process.

	David

-- 
David Laight: david@l8s.co.uk