linux kill children process

来源:互联网 发布:小米6数据断流官方回应 编辑:程序博客网 时间:2024/06/07 20:09

最近做项目,需要写脚本,根据某一个process id kill 掉该进程以及其下的所有子进程。

代码如下:

#!/bin/sh


root=$1


if [[ -z $root ]]; then
   echo "No PID Given."
   exit;
fi




treekill()
{
    local father=$1


    # children
    children=`ps -ef| awk '$3 == '$father' { print $2 }'`
    if [ ${#children[@]} -ne 0 ]
    then
        for child in ${children[*]}
        do
            treekill $child
        done
    fi


    # father
    kill -9 $father
    #echo " $father killed !"
}


treekill $root