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

  • 3
  • and related: https://unix.stackexchange.com/questions/14815/process-descendants/14853#14853 https://unix.stackexchange.com/questions/124127/kill-all-descendant-processes/124130#124130 – Bart Aug 14 '19 at 09:45
  • 1
    I see this has also been asked at https://stackoverflow.com/questions/57491869/bash-kill-process-and-all-subprocesses-without-using-group-id It's probably best to remove the question from one of the sites as cross-posted questions are closed on [unix.se]. – Anthony Geoghegan Aug 14 '19 at 10:34
  • @Bart Seems like all the solutions in those threads have one of the 4 issues I address in my post – quantumbutterfly Aug 15 '19 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 Aug 15 '19 at 23:46
  • okay so I created another question with more context on what I am trying to accomplish https://unix.stackexchange.com/questions/535815/killing-a-process-running-executed-with-sudo-without-using-sudo-in-kill-command – quantumbutterfly Aug 15 '19 at 23:52

0 Answers0