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:
Killing via group id. The child may have sibling processes with the same group id I do not want to kill.
Using
kill $childpid
. This is not killing all grandchildren and great grandchildren processesUsing
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 fileUsing
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
pkill -P
may do fine if the processes don't double-fork and if your script takes care of following the hierarchy. Wrt point 4, it might be just a matter of sending a terminating signal that your processes don't catch nor ignore. For instance, by default the so-called "real-time signals" are all terminating signals and have no standard defined meaning, I would think there is one available for your purpose. Also, what process spawns the grandchildren ? again a shell script ? – LL3 Aug 15 '19 at 23:46