Kill bash and child process

て烟熏妆下的殇ゞ 提交于 2019-12-11 11:07:40

问题


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);

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

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?


回答1:


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]*)

Also, more answers on this post : Best way to kill all child processes




回答2:


from man 2 kill:

If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid.

That is you may kill all children, direct or indirect which weren't created its own process group.



来源:https://stackoverflow.com/questions/23032296/kill-bash-and-child-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!