How to kill all subprocesses of shell?

2019-01-17 10:01发布

I'm writing a bash script, which does several things.

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 were 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?

6条回答
女痞
2楼-- · 2019-01-17 10:17

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

kill -- -1234

查看更多
孤傲高冷的网名
3楼-- · 2019-01-17 10:27
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

查看更多
太酷不给撩
4楼-- · 2019-01-17 10:28

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)

查看更多
不美不萌又怎样
5楼-- · 2019-01-17 10:36
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)
查看更多
疯言疯语
6楼-- · 2019-01-17 10:36

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/

查看更多
我命由我不由天
7楼-- · 2019-01-17 10:40

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.

查看更多
登录 后发表回答