Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A recursive process kill script

22 views
Skip to first unread message

Sunil Alankar

unread,
May 28, 2004, 5:18:18 PM5/28/04
to
Here is a utility I wrote for killing the process tree of a parent
process recursively. Useful in automation.

#!/bin/bash

##
# recursive kill. kills the process tree down from the specified pid
#

# foreach child of pid, recursive call dokill
dokill() {
local pid=$1
local itsparent=""
local aprocess=""
local x=""
# next line is a single line
for x in `/bin/ps -f | sed -e '/UID/d;s/[a-zA-Z0-9_-]\{1,\}
\{1,\}\([0-9]\{1,\}\) \{1,\}\([0-9]\{1,\}\) .*/\1 \2/g'`
do
if [ "$aprocess" = "" ]; then
aprocess=$x
itsparent=""
continue
else
itsparent=$x
if [ "$itsparent" = "$pid" ]; then
dokill $aprocess
fi
aprocess=""
fi
done
echo "killing $1"
kill -9 $1 > /dev/null 2>&1
}


case $# in
1) PID=$1
;;
*) echo "usage: rekill <top pid to kill>";
exit 1;
;;
esac

dokill $PID

0 new messages