[英]Kill bash and child process
我的C程序在bash shell中执行命令。 为此,我fork
并在子进程中运行:
char* command = "..."; /* provided by user */
execlp("/bin/bash", "bash", "-c", command, NULL);
如果这是一个长期运行的命令,我想选择杀死它。 例如,说我在做:
execlp("/bin/bash", "bash", "-c", "find / test", NULL);
此后,我知道正在执行bash的孩子的PID,但是bash正在分叉一个单独的进程来执行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),但是find进程(7803)仍然继续执行。 我如何确保bash进程将信号传播到其子进程?
它将SIGTERM发送到传入参数的进程组ID以及所有子级。
kill -- -$(ps -o pgid= $PID | grep -o [0-9]*)
另外,有关此帖子的更多答案: 杀死所有子进程的最佳方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.