核心服務(wù)器上跑了一堆的腳本、程序,難免有時(shí)候會(huì)出現(xiàn)僵尸進(jìn)程,死不死活不活的在那里占用資源,最初只是寫了個(gè)根據(jù)關(guān)鍵字查殺進(jìn)程的linux shell腳本,后來發(fā)現(xiàn)很多時(shí)候進(jìn)程死在那里的時(shí)候其實(shí)是內(nèi)部調(diào)用子進(jìn)程的時(shí)候出現(xiàn)了問題,這時(shí)候光殺父進(jìn)程根本沒解決根本問題。比如說rsync的時(shí)候通過ssh來連接,rsync本身沒問題,但可能ssh死掉了。因此重新寫了腳本,遞歸查找子進(jìn)程。
#!/bin/sh
# 遞歸找到導(dǎo)致進(jìn)程僵死的最底層子進(jìn)程并殺除.
ParentProcessID=$1;
if [ "x${ParentProcessID}" = "x" ] ; then
echo "Please Supply the top Parent Process ID to be killed!"
echo "Usage:sh $0 PID [-v]"
echo "PID The Parent Process ID as root"
echo "-v is this argument supplied,no real kill operation will be performed,only process tree be show."
exit 1
fi
let IsRealKillDo=1;
if [ "x$2" = "x-v" ] ; then
let IsRealKillDo=0;
fi
echo "Begin Kill the Leaf Process of process ${ParentProcessID}" >2
killpidList=""
function loopNextSubProcess(){
local nParentProcessID=$1
local tmpPidList=""
tmpPidList=`ps -A --format='%p%PisParent' --width 2048 -w --sort pid|grep "${nParentProcessID}isParent"|grep -v grep|grep -v "$$" | awk '{ printf $1 }'`
ps --format='%p%P%a' --width 2048 -w -p ${nParentProcessID}|grep -v grep|grep -v "$$" >2
if [ "x${tmpPidList}" = "x" ] ; then
echo "****Got One Leaf = [${nParentProcessID}]****" >2
killpidList="${killpidList}\n${nParentProcessID}"
return
fi
for theNextPid in ${tmpPidList} ; do
loopNextSubProcess ${theNextPid}
done
}
loopNextSubProcess ${ParentProcessID}
if [ ${IsRealKillDo} -eq 1 -a "x${killpidList}" != "x" ] ; then
for curpid in `echo -e ${killpidList}` ; do
if [ "x${curpid}" != "x" ] ; then
echo "kill -9 ${curpid}"
kill -9 ${curpid}
fi
done
else
echo -e ${killpidList}
fi