From: smr@iti.org (Stephen Riehm)
Newsgroups: comp.unix.questions
Subject: Re: Script for killing mutiple processes?
Date: 22 Jul 92 14:09:25 GMT

odkahn@eos.ncsu.edu (Opher D. Kahn) writes:

>with 10-15 child processes that need to be killed.  Is it possible
>to somehow pipe the process numbers from 'ps -aux | grep dood' into
>a kill command, so that I don't have to kill each one of them
>manually???  (dood is the name of the process).

for a whlie I have had the following script lying around.. it might be
of some help to you... ( OK its csh, but I wrote it before I got to
like /bin/sh and I can't be bothered converting it blah blah blah )

Some notes:
	There is no warrantee, it will always prompt, if you kill jobs
	    its your problem!
	The default answer is always YES, so just hitting return WILL
	    kill the prompted job. Some people don't like this, I do ;-)
	I don't use ps to just list the command name, for the simple
	    reason that this can be used to kill a particular vi session
	    that got hung which editing a given file ( believe me this
	    is possible! ) ( ie: slay filename )
	This is for use with sysv style machines at the moment, I
	    believe the right options for bsd are ps -glx, but you will
	    have to change all the cuts etc etc

catchya

-----------------------------------------------------------------
Stephen Riehm				   smr@pki-nbg.philips.de
not in any way talking on behalf of:
Philips Kommunikations Industrie	  Phone: +49 911 526 2975
8500 Nuremberg, Germany			    Fax: +49 911 526 2095
I come from the land down under, where women glow and men chunder

--- cut here --- file: slay
[nosave]
#!/bin/csh -f
#
# cshell script to kill jobs of a particular name
#
# Usage:	slay <jobname>...
#
# Written By:	Stephen Riehm		( smr@pki-nbg.philips.de )
#
# Permission to copy and distribute this file is granted, see the GNU
# Free Software Copyright file for details.
#
#
# if no jobs specified, try to remove defunct jobs if possible
#
if ( $#argv == 0 ) then
    set argv="defunct"
endif

foreach i ( $argv )

#
# find all the jobs owned by the current user with $i in their command string
#
    foreach j ( `ps -u $LOGNAME | grep $i | grep -v grep | cut -c1-7` )
	set js=`ps -fp $j | grep -v PPID`
	set jd=`echo $js | cut -d'<' -f2`

#
# get the parent ID of defunct processes
#
	if ( "$jd" == "defunct>" ) then
	    set p=`echo $js | cut -c16-20`
	    set jp=`ps -fp $p | grep -v PPID`
	    echo $jp
	    echo -n $j is defunct, kill it's parent $p ' ? [y]'
	else
	    echo $js
	    echo -n kill $j '? [y]'
	endif
	set q=$<
	if ( $q == "n" || $q == "no" ) then
	    echo $j left to run free
	else if ( $q == "q" ) then
#
# short cut if you don't want to kill any jobs after all
# ( undocumented feature... best kind! )
#
	    exit
	else
#
# try a nice kill first
#
	    ( kill $j )
#
# wait for something to happen ( you have 1 second to pack up and leave )
#
	    sleep 1
#
# if the process still exists, ask for permission to go 'ALL OUT'
#
	    if ( `ps -p $j | wc -l` != "1"  ) then
		echo -n "$j didn't die... shall I nuke it?? [y]"
		set q=$<
		if( $q == "n" ) then
		    echo ""
		    echo "O.K. have it your way!"
		else
		    ( kill -9 $j )
		    echo $j killed
		endif
	    else
		echo $j killed
	    endif
	endif
    end
end