Shell指令碼實現查殺子進程、殭屍進程_linux shell

來源:互聯網
上載者:User

核心伺服器上跑了一堆的指令碼、程式,難免有時候會出現殭屍進程,死不死活不活的在那裡佔用資源,最初只是寫了個根據關鍵字查殺進程的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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.