I found this script that kills all child processes for a given PID
Code:
# This script will kill all the child process id for a given pid
#Store the current Process ID
CURPID=$$
# This is process id, parameter passed by user
ppid=$1
if [ -z $ppid ] ; then
echo No PID given.
exit;
fi
arraycounter=1
while true
do
FORLOOP=FALSE
# Get all the child process id
for i in `ps -ef| awk '$3 == '$ppid' { print $2 }'`
do
if [ $i -ne $CURPID ] ; then
procid[$arraycounter]=$i
arraycounter=`expr $arraycounter + 1`
ppid=$i
FORLOOP=TRUE
fi
done
if [ "$FORLOOP" = "FALSE" ] ; then
arraycounter=`expr $arraycounter - 1`
## We want to kill child process id first and then parent id's
while [ $arraycounter -ne 0 ]
do
kill -9 "${procid[$arraycounter]}" >/dev/null
arraycounter=`expr $arraycounter - 1`
done
exit
fi
done
## Kill Parent ID
kill -9 $CURPID
I would only use kill -9 as a last resort.
I usually start with:
wait a few seconds
if it is still alive:
wait a few seconds
if it is still alive:
Now it should be dead and gone, but there is always the last, dirty resort:
Note, that kill -9 cuts the throat without giving the sick process any chance of cleaning up after itself (sockets/temp/children a.o.)
Bookmarks