Quote:
Originally Posted by Valery Reznic
I was under impression, that it's needed for kill bash script (i.e non-interactive bash) and all processes that this script invoked and that it's done from another shell.
|
When called in the usual way (from an interactive shell prompt), a non-interactive script is the group leader. So if your script looks like:
Code:
#!/bin/sh
sleep 60 & sleep 60 & sleep 60 & wait
And you execute it at a terminal,
Then you can use another shell to do
Code:
$ kill -- -$(pgrep test.sh)
Then everything works as expected and the setsid is unnecessary.
If, however, you are running a (non-shell) program or script that launches your script, then it may be necessary, as
Code:
$ kill -- -${PGID_of_test.sh}
or even
from inside the test.sh will kill all processes with that PGID (most likely including the parent).
Note, however, that killing by group pid is not a strict parent-child relationship. For example, in the following script, the last sleep does not die with “
kill -- -${PGID_of_test.sh}”:
Code:
#!/bin/sh
sleep 60 & sleep 60 & setsid sleep 65 & wait
If you want to kill all a script’s direct descendants, you can use pkill:
Code:
pkill -P $(pgrep test.sh)
Also note that pkill, pgrep, and setsid are utilities found normally on linux distributions, but might not be available on other unices.