java 进程 递归kill_Shell脚本实现查杀子进程、僵尸进程

#!/bin/sh

# 递归找到导致进程僵死的最底层子进程并杀除.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值