午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    Shell腳本實現(xiàn)查殺子進程、僵尸進程
    來源:易賢網(wǎng) 閱讀:814 次 日期:2015-01-30 14:04:09
    溫馨提示:易賢網(wǎng)小編為您整理了“Shell腳本實現(xiàn)查殺子進程、僵尸進程”,方便廣大網(wǎng)友查閱!

    核心服務(wù)器上跑了一堆的腳本、程序,難免有時候會出現(xiàn)僵尸進程,死不死活不活的在那里占用資源,最初只是寫了個根據(jù)關(guān)鍵字查殺進程的linux shell腳本,后來發(fā)現(xiàn)很多時候進程死在那里的時候其實是內(nèi)部調(diào)用子進程的時候出現(xiàn)了問題,這時候光殺父進程根本沒解決根本問題。比如說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

    更多信息請查看IT技術(shù)專欄

    更多信息請查看腳本欄目
    易賢網(wǎng)手機網(wǎng)站地址:Shell腳本實現(xiàn)查殺子進程、僵尸進程

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)