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
使用遞歸調用實現(xiàn)了樹的深度優(yōu)先遍歷:
遞歸條件是 childs 數(shù)組不空, 且第一個元素非空( awk 在葉子上返回一個第一個元素是空的數(shù)組)
base line condition 設置在葉子進程上進行遞歸返回.
getchld_kill 的參數(shù)是樹根進程.