杀了bash和子进程 [英] Kill bash and child process
问题描述
我的C程序在bash shell中执行命令。要做到这一点,我叉
和子进程我运行:
My C program executes commands in a bash shell. To do this, I fork
and in the child process I run:
char* command = "..."; /* provided by user */
execlp("/bin/bash", "bash", "-c", command, NULL);
如果这是一个长期运行的命令,我想有杀死它的选项。例如,说我这样做的:
If this is a long running command, I would like to have the option of killing it. For example, say I'm doing:
execlp("/bin/bash", "bash", "-c", "find / test", NULL);
在此之后,我知道,正在执行的bash的子进程的PID,但bash由派生一个单独的进程来执行的发现。
After this, I know the PID of the child that is executing bash, but bash is forking a separate process to execute find.
$ ps aux | grep find
zmb 7802 0.0 0.1 5252 1104 pts/1 S+ 11:17 0:00 bash -c find / test
zmb 7803 0.0 0.0 4656 728 pts/1 S+ 11:17 0:00 find / test
我可以杀死bash进程(7802),但发现过程(7803)仍继续执行。我怎样才能确保bash进程传播信号,它的孩子吗?
I can kill the bash process (7802), but the find process (7803) still continues to execute. How can I ensure that the bash process propagates signals to it's children?
推荐答案
这会SIGTERM发送到参数传递的进程组ID和所有的孩子。
It will send a SIGTERM to the Process Group ID passed in parameter and to all children.
kill -- -$(ps -o pgid= $PID | grep -o [0-9]*)
另外,对这个职位更多的答案:最好的方式杀死所有的子进程
这篇关于杀了bash和子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!