Kill bash and child process

The My C program executes commands in the bash shell. For 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 team, I would like to be able to kill her. For example, let's say what I do:

 execlp("/bin/bash", "bash", "-c", "find / test", NULL); 

After that, I know the PID of the child executing bash, but bash does the deployment of a separate process to do the search.

 $ 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 (7803) process is still running. How can I ensure that the bash process propagates signals to children for it?

+1
source share
2 answers

It will send SIGTERM to the process group identifier passed in the parameter and to all child elements.

 kill -- -$(ps -o pgid= $PID | grep -o [0-9]*) 

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

+2
source

from man 2 kill :

If pid is less than -1, then sig is sent to each process in the process group whose identifier is -p.

That is, you can kill all children, direct or indirect, who have not created their own group of processes.

+2
source

All Articles