国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

Shell腳本實現查殺子進程、僵尸進程

2019-10-26 18:44:57
字體:
來源:轉載
供稿:網友

核心服務器上跑了一堆的腳本、程序,難免有時候會出現僵尸進程,死不死活不活的在那里占用資源,最初只是寫了個根據關鍵字查殺進程的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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 六安市| 潼关县| 娱乐| 仙居县| 谷城县| 林西县| 利川市| 红桥区| 尉犁县| 长治市| 柘城县| 拜城县| 广安市| 绥宁县| 浦江县| 齐齐哈尔市| 会东县| 柯坪县| 益阳市| 修水县| 乐昌市| 沽源县| 株洲县| 内黄县| 黎城县| 开封市| 柳林县| 饶平县| 竹北市| 屏东县| 曲水县| 灵武市| 东方市| 桓台县| 山丹县| 衡山县| 南阳市| 开平市| 河间市| 铅山县| 黎城县|