杀死所有子进程:
pkill -P $$
$$ 为 目标 pid
另一种情况是你可能想要杀死当前 shell 进程的所有后代以及直接子进程。在这种情况下,你可以使用下面的递归 shell 函数列出所有后代 PID,然后将它们作为参数传递给 kill:
list_descendants ()
{
local children=$(ps -o pid= --ppid "$1")
for pid in $children
do
list_descendants "$pid"
done
echo "$children"
}
kill $(list_descendants $$)