杀死进程树

function getchld_kill() { if [[ -z "$1" ]]; then echo "arguemnt 1 should be a number"; exit; fi local child local father=$1 #echo"father: ${father}" local childs=$(ps -ef | awk -v father=$father 'BEGIN{ ORS=" "; } $3==father{ print $2; }') #echo "childs: " ${childs[*]} if [[ ${#childs[@]} -ne 0 && -n "${childs[0]}" ]]; then for child in ${childs[@]} do #echo child:${child} getchld_kill ${child} done echo "killing the father: ${father}" kill -9 ${father} else echo "killing myself pid:${father}" kill -9 ${father} return fi }getchld_kill $1

【杀死进程树】使用递归调用实现了树的深度优先遍历:
递归条件是 childs 数组不空, 且第一个元素非空( awk 在叶子上返回一个第一个元素是空的数组)
base line condition 设置在叶子进程上进行递归返回.
getchld_kill 的参数是树根进程.

    推荐阅读