I'm writing bash script, which does several thing.

In the beginning it starts several monitor scripts, each of them runs some other tools.

At the end of my main script, I would like to kill all things that spawned from my shell.

So, it might looks like this:

#!/bin/bash

some_monitor1.sh &
some_monitor2.sh &
some_monitor3.sh &

do_some_work
...

kill_subprocesses

The thing is that most of these monitors spawn their own subprocesses, so doing (for example): killall some_monitor1.sh will not always help.

Any other way to handle this situation?

share|improve this question
    
A great thread about this: stackoverflow.com/questions/392022/… – nandinga Jul 22 '16 at 17:01
up vote 17 down vote accepted

After starting each child process, you can get its id with

ID=$!

Then you can use the stored PIDs to find and kill all grandchild etc. processes as described here or here.

share|improve this answer
    
Just to add, if the monitors' subprocesses can spawn subsubprocesses etc., you'd need to use the technique described in the link recursively. – David Z Apr 11 '10 at 19:49
    
@David the 2nd page referred includes a solution for that. – Péter Török Apr 11 '10 at 19:52
    
Yes, but that second link hadn't yet appeared when I wrote my comment. – David Z Apr 11 '10 at 21:47
    
@David I thought so too. I didn't mean to counterargument or anything, sorry if it sounded like that. – Péter Török Apr 11 '10 at 22:21
    
Not to worry, it didn't :-) (by the way: +1) – David Z Apr 12 '10 at 1:08

Extending pihentagy's answer to recursively kill all descendants (not just children):

kill_descendant_processes() {
    local pid="$1"
    local and_self="${2:-false}"
    if children="$(pgrep -P "$pid")"; then
        for child in $children; do
            kill_descendant_processes "$child" true
        done
    fi
    if [[ "$and_self" == true ]]; then
        kill -9 "$pid"
    fi
}

Now

kill_descendant_processes $$

will kill descedants of the current script/shell.

(Tested on Mac OS 10.9.5. Only depends on pgrep and kill)

share|improve this answer
2  
Ok, but please don't use kill -9"by default", give a change the processes to exit! serverfault.com/a/415744/55046 – pihentagy May 12 '15 at 16:40
kill $(jobs -p)

Rhys Ulerich's suggestion:

Caveat a race condition, using [code below] accomplishes what Jürgen suggested without causing an error when no jobs exist

[[ -z "$(jobs -p)" ]] || kill $(jobs -p)
share|improve this answer
3  
Caveat a race condition, using 'test -z "`jobs -p`" || kill `jobs -p`' accomplishes what Jürgen suggested without causing an error when no jobs exist – Rhys Ulerich Jul 13 '11 at 19:37
pkill -P $$

will fit (just kills it's own descendants)

EDIT: I got a downvote, don't know why. Anyway here is the help of -P

   -P, --parent ppid,...
          Only match processes whose parent process ID is listed.

and $$ is the process id of the script itself

share|improve this answer
4  
Why was this answer downvoted? Seems like a sensible answer, and I'm considering using it. Are there any drawbacks I'm not aware of? – MestreLion Sep 23 '13 at 15:41
1  
This works for me on Ubuntu 13.04. – Nordlöw Oct 4 '13 at 10:47
1  
@Chexpir: It's not part of bash. However, if you system does not have it, you are in deep5h1t. It's part of the procps package under Ubuntu, and this package contains other core utilities, like free, kill and ps. – pihentagy Jul 29 '14 at 16:12
1  
@pihentagy Isn't there a race condition? pkill itself is a child of current shell. Therefor, will "pkill -P $$" kill the "pkill" process before all other processes being killed? – Lungang Fang Nov 24 '15 at 7:07
8  
No, there is no race condition here. pkill is a symlink to pgrep and in pgrep.c on line 441 you can see that pkill will skip itself when checking for processes to kill. So after killing every other child, the pkill command will terminate regularly. – fxtentacle Jun 16 '16 at 9:09

pkill with optioin "-P" should help:

pkill -P $(pgrep some_monitor1.sh)

from man page:

   -P ppid,...
          Only match processes whose parent process ID is listed.

There are some discussions on linuxquests.org, please check:

http://www.linuxquestions.org/questions/programming-9/use-only-one-kill-to-kill-father-and-child-processes-665753/

share|improve this answer
    
What if you have had started it 2 times? One will kill the others subprocesses! – pihentagy Jul 12 '13 at 12:41

If you use a negative PID with kill it will kill a process group.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.