killing processes thru' C programs

Doug Gwyn gwyn at smoke.brl.mil
Wed May 1 02:22:06 AEST 1991


In article <12582 at dog.ee.lbl.gov> torek at elf.ee.lbl.gov (Chris Torek) writes:
>In article <1991Apr24.144240.3322 at uoft02.utoledo.edu>
>grx0736 at uoft02.utoledo.edu writes:
>>does anyone know how to kill processes through C programs if only
>>the name of the running process is known, and not its pid.
>The `name' of a process *is* its pid.

While I was tempted to respond in much the same way, actually it is
possible to interpret the original request in a way that might make
enough sense to have an adequate solution.

#!/bin/sh
#	zap -- kill all processes matching pattern
#	adapted from Kernighan & Pike
HEAD=head	# see below
PICK=pick	# see below
IFS='
'
sig=
case $1 in
"")	echo 'Usage: zap [-2] pattern' 1>&2; exit 1;;
-*)	sig=$1; shift;;
esac
# BSD flavor of "ps" assumed:
ps -ag | $HEAD 1
exec kill $sig `$PICK \`ps -ag | egrep "$*" | egrep -v 'zap|egrep'\` | awk '{print $1}'`

#!/bin/sh
#	head -- print first few lines of file
if [ $# -eq 0 ]
then	n=10
else	case $1 in
	[0-9]*)	n=$1;	shift;;
	*)	n=10;;
	esac
fi
exec sed -e ${n}q $*

#!/bin/sh
#	pick -- select arguments
#	adapted from Kernighan & Pike
for i
do	echo -n "$i? " > /dev/tty	# BSD or Research UNIX "echo" assumed
	read response
	case $response in
	[yY]*)	echo $i;;
	[qQ]*)	break;;
	esac
done < /dev/tty



More information about the Comp.unix.questions mailing list