编程语言


Shell脚本实现查杀子进程、僵尸进程

网络编程 Shell脚本实现查杀子进程、僵尸进程 06-22

核心服务器上跑了一堆的脚本、程序,难免有时候会出现僵尸进程,死不死活不活的在那里占用资源,最初只是写了个根据关键字查杀进程的linux shell脚本,后来发现很多时候进程死在那里的时候其实是内部调用子进程的时候出现了问题,这时候光杀父进程根本没解决根本问题。比如说rsync的时候通过ssh来连接,rsync本身没问题,但可能ssh死掉了。因此重新写了脚本,递归查找子进程。

#!/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

Shell脚本实现乱序排列文件内容的多种方法(洗牌问题)
洗牌问题:洗一副扑克,有什么好办法?既能洗得均匀,又能洗得快?即相对于一个文件来说怎样高效率的实现乱序排列?ChinaUnix确实是Shell高手云集的

Shell脚本实现生成SSL自签署证书
启用apache的mod_ssl之后需要有证书才能正常运作。写了个脚本来操作。首先要确定机器上已经有openssl。#!/bin/sh##ssl证书输出的根目录。sslOutputRoot="/etc/apach

Shell脚本中非交互式修改密码的两种方法
今天在写一个shell脚本的时候发现需要设置用户的密码,而设置密码是交互式的,这样在脚本中显然是不太好的,因此上网查找资料,发现了以下两种常


编辑:编程语言

标签:脚本,进程,递归,写了,两种