TL;DR, I need a way to kill a process and all descendant processes, without killing siblings with the same group id, without output printed in the terminal, and with a non-zero exit code.

I have a bash script which is spawning child processes, which spawn grandchild and great grandchild processes.

I want to kill a particular child, and all it's descendants, without using sudo.

Here are some solutions I've tried that don't work for me:

  1. Killing via group id. The child may have sibling processes with the same group id I do not want to kill.

  2. Using kill $childpid. This is not killing all grandchildren and great grandchildren processes

  3. Using kill -9 $childpid. This leads to text output in terminal that I ran the script from, which is not ideal, even if the script and the kill command both have their stdout and stderr routed to a different file

  4. Using pkill -P $childpid. This seems to do what I want at first, but the child process exits with exit code zero. The parent process needs to know its child was killed prematurely, so I need it to exit with a code other than 0

What is the best way to do this?

Edit: my post has been flagged as a possible duplicate, but all solutions in those links have one of the issues I describe in my post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.