how to: using bash to kill a parent process and all spawned child processes

I got this project in a Linux environment where I have to terminate a process before running one. Sounds easy at first glance, it’s very easy to kill a process in Linux, all I have to do is get the process’ PID (process id) and terminate it using the kill command…. HA! A no-brainer problem!

But after thinking about it for a few minutes, it hit me, it’s possible that the process that I want to terminate can spawn child processes, and it’s child processes can spawn another set of child processes… and so on… and so forth… and I have to terminate all these child processes too! Aaarrggh!

And so, all because I’m lazy, I opened up Google and did a little script hunting. I found some tips but it doesn’t fit my needs. Most solutions were how to kill one level of child processes only (or maybe I did not try hard enough). After a few hours wasted on looking for a “already-done-by-others” solution, I gave up and decided to write my own….

So much for being lazy… So, on to the drawing board…
Well, I hope you can make sense of what’s the diagram above (don’t ask me how I came up with it…). If you don’t know what it is, just believe me when I say it’s a tree.

The behaviour of a process spawning child processes can be described by a tree. Based on this, all we have to do is determine the nodes at each depth. The idea is to store process ids in an array based on what depth in the process tree they belong. After generating the array, we can decide if we want to terminate processes from parent to child or vice versa. In my case, I have to terminate from parent to child.

Based on that gibberish idea above, I managed to write the following code:




while [ $quit -eq 0 ]

    # get all child processes spawned by this/these ppid/s
    ids[$index]=$(ps -o pid --ppid ${ids[$index-1]} | \
      pcregrep '\d+' | tr \\n ' ')

    # if no child processes found
    if [ ! "${ids[$index]}" ]
        # quit

# kill process from parent to all child processes
for i in $(seq 0 ${#ids[@]})
    if [ "${ids[$i]}" ]
        kill ${ids[$i]}

The code above assumes that the root process id is known. You may have to do some checking first if you have a valid root process id as parameter.

And that’s it! I’m just hoping that my laziness can appreciate fruition…


2 thoughts on “how to: using bash to kill a parent process and all spawned child processes

  1. Cristian


    I need to use your script, but I get an error when I execute it:

    ./ line 7: [: -eq: unary operator expected
    Any ideas?


    1. tar Post author

      Oops. Sorry, I updated the code. quit=0 should be in the next line. Let me know if you still encounter problems 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s