Bash: 杀死子进程中的所有进程
Bash: Killing all processes in subprocess
在bash中我可以通过$!
变量得到最后一个子进程的进程ID(pid
)。然后我可以在它完成之前杀死这个子进程:
(sleep 5) & pid=$!
kill -9 $pid
这与宣传的一样有效。如果我现在在 sleep
之后用更多命令扩展子进程,sleep
命令会在子进程被终止后继续,即使其他命令永远不会执行。
举个例子,考虑下面的例子,它启动了一个子进程并使用 ps
监视它的暗杀:
# Start subprocess and get its pid
(sleep 5; echo done) & pid=$!
# grep for subprocess
echo "grep before kill:"
ps aux | grep "$pid\|sleep 5"
# Kill the subprocess
echo
echo "Killing process $pid"
kill -9 $pid
# grep for subprocess
echo
echo "grep after kill:"
ps aux | grep "$pid\|sleep 5"
# Wait for sleep to finish
sleep 6
# grep for subprocess
echo
echo "grep after sleep is finished:"
ps aux | grep "$pid\|sleep 5"
如果我将其保存到一个名为 filename
的文件中,并且将其命名为 运行,我将得到以下打印输出:
grep before kill:
username 7464 <...> bash filename
username 7466 <...> sleep 5
username 7467 <...> grep 7464\|sleep 5
Killing process 7464
grep after kill:
username 7466 <...> sleep 5
username 7469 <...> grep 7464\|sleep 5
grep after sleep is finished:
username 7472 <...> grep 7464\|sleep 5
其中来自 ps
命令的不重要信息被替换为 <...>
。看起来 kill
已经杀死了 filename
的整体 bash 执行,同时留下 sleep
运行ning.
如何正确终止整个子进程?
你可以看看 rkill
似乎符合你的要求:
http://www.unix.com/man-page/debian/1/rkill/
rkill [-SIG] pid/name...
When invoked as rkill, this utility does not display information about the processes, but
sends them all a signal instead. If not specified on the command line, a terminate
(SIGTERM) signal is sent.
您可以在子 shell 中设置陷阱以在退出前终止所有活动作业:
(trap 'kill $(jobs -p)' EXIT; sleep 5; echo done ) & pid=$!
我不知道为什么那个睡眠进程会被孤立,无论如何你可以使用带有 -P 标志的 pkill 来 also kill all children
pkill -TERM -P $pid
编辑:
这意味着为了杀死一个进程及其所有 children 你应该使用
CPIDS=`pgrep -P $pid` # gets pids of child processes
kill -9 $pid
for cpid in $CPIDS ; do kill -9 $cpid ; done
在bash中我可以通过$!
变量得到最后一个子进程的进程ID(pid
)。然后我可以在它完成之前杀死这个子进程:
(sleep 5) & pid=$!
kill -9 $pid
这与宣传的一样有效。如果我现在在 sleep
之后用更多命令扩展子进程,sleep
命令会在子进程被终止后继续,即使其他命令永远不会执行。
举个例子,考虑下面的例子,它启动了一个子进程并使用 ps
监视它的暗杀:
# Start subprocess and get its pid
(sleep 5; echo done) & pid=$!
# grep for subprocess
echo "grep before kill:"
ps aux | grep "$pid\|sleep 5"
# Kill the subprocess
echo
echo "Killing process $pid"
kill -9 $pid
# grep for subprocess
echo
echo "grep after kill:"
ps aux | grep "$pid\|sleep 5"
# Wait for sleep to finish
sleep 6
# grep for subprocess
echo
echo "grep after sleep is finished:"
ps aux | grep "$pid\|sleep 5"
如果我将其保存到一个名为 filename
的文件中,并且将其命名为 运行,我将得到以下打印输出:
grep before kill:
username 7464 <...> bash filename
username 7466 <...> sleep 5
username 7467 <...> grep 7464\|sleep 5
Killing process 7464
grep after kill:
username 7466 <...> sleep 5
username 7469 <...> grep 7464\|sleep 5
grep after sleep is finished:
username 7472 <...> grep 7464\|sleep 5
其中来自 ps
命令的不重要信息被替换为 <...>
。看起来 kill
已经杀死了 filename
的整体 bash 执行,同时留下 sleep
运行ning.
如何正确终止整个子进程?
你可以看看 rkill
似乎符合你的要求:
http://www.unix.com/man-page/debian/1/rkill/
rkill [-SIG] pid/name...
When invoked as rkill, this utility does not display information about the processes, but sends them all a signal instead. If not specified on the command line, a terminate (SIGTERM) signal is sent.
您可以在子 shell 中设置陷阱以在退出前终止所有活动作业:
(trap 'kill $(jobs -p)' EXIT; sleep 5; echo done ) & pid=$!
我不知道为什么那个睡眠进程会被孤立,无论如何你可以使用带有 -P 标志的 pkill 来 also kill all children
pkill -TERM -P $pid
编辑: 这意味着为了杀死一个进程及其所有 children 你应该使用
CPIDS=`pgrep -P $pid` # gets pids of child processes
kill -9 $pid
for cpid in $CPIDS ; do kill -9 $cpid ; done