Reputation: 7867
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?
Upvotes: 1
Views: 1192
Reputation: 1140
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
Upvotes: 2
Reputation: 17455
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.
Upvotes: 3