3

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

6
  • 3
    Possible duplicate of Kill all descendant processes
    – Bart
    Commented Aug 14, 2019 at 9:45
  • 1
    I see this has also been asked at stackoverflow.com/questions/57491869/… It's probably best to remove the question from one of the sites as cross-posted questions are closed on Unix & Linux. Commented Aug 14, 2019 at 10:34
  • @Bart Seems like all the solutions in those threads have one of the 4 issues I address in my post Commented Aug 15, 2019 at 21:50
  • 1
    @quantumbutterfly A 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
    Commented Aug 15, 2019 at 23:46

0

You must log in to answer this question.

Browse other questions tagged .