[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-bash] How to kill all children oa script
From: |
Eduardo A . Bustamante López |
Subject: |
[Help-bash] How to kill all children oa script |
Date: |
Wed, 11 Mar 2015 04:49:02 -0600 |
User-agent: |
Mutt/1.5.20 (2009-12-10) |
Keeping track of all the processes spawned by the current shell is not fun. I
use this:
trap 'kill -TERM -$$' EXIT
as a quick way of killing all processes spawned by the current script (since
they all have the same pgid, unless they change it explicitly, which is usually
not the case).
But today I learned that this doesn't work when you have job control enabled:
# without job control
address@hidden/local/src/bash% ./bash -c 'sleep 5 & echo $! > x; kill -TERM
-$$'; ps -fp $(<x)
zsh: terminated ./bash -c 'sleep 5 & echo $! > x; kill -TERM -$$'
UID PID PPID C STIME TTY TIME CMD
# with job control
address@hidden/local/src/bash% ./bash -mc 'sleep 5 & echo $! > x; kill -TERM
-$$'; ps -fp $(<x)
zsh: terminated ./bash -mc 'sleep 5 & echo $! > x; kill -TERM -$$'
UID PID PPID C STIME TTY TIME CMD
dualbus 10459 1 0 04:35 pts/45 00:00:00 sleep 5
Then I went to the manual, and found this:
> To facilitate the implementation of the user interface to job control, the
> operating
> system maintains the notion of a current terminal process group ID.
> Members of this
> process group (processes whose process group ID is equal to the current
> terminal pro-
> cess group ID) receive keyboard-generated signals such as SIGINT. These
> processes are
> said to be in the foreground. Background processes are those whose process
> group ID
> differs from the terminal’s; such processes are immune to
> keyboard-generated signals.
> Only foreground processes are allowed to read from or, if the user so
> specifies with
> stty tostop, write to the terminal. Background processes which attempt to
> read from
> (write to when stty tostop is in effect) the terminal are sent a SIGTTIN
> (SIGTTOU) sig-
> nal by the kernel’s terminal driver, which, unless caught, suspends the
> process.
So, basically, background processes are in a different pgid, and that's the
reason my kill command didn't terminate the sleep when job control was enabled.
What's the recommended way to achieve the same effect, when job control is
enabled? Should I use jobs -p?
Thanks,
Eduardo
- [Help-bash] How to kill all children oa script,
Eduardo A . Bustamante López <=