分类:
2009-12-17 19:42:17
杀掉给定PID进程的所有子进程,Linux下面可以通过pkill -P PID来实现,但是
AIX下面则没有pkill这个命令,网上google到了下面的脚本,可以实现这个功能:
#!/bin/ksh
# 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