Use only one "kill" to kill father and child processes
Hi!
Is there a way to kill a bash process so it will automatically kill all the programs that it initiated? Currently I trap the signal in the bash process and kill each program individually, but I was wondering if there is an easier way. Thanks for the help, Xosen. |
Quote:
|
Quote:
setsid <your_program> setsid will create new process group Then instead of kill PID you can use kill -- -PID to kill all processes in the group |
You don’t necessarily need to use setsid, as an interactive bash process is already its own session leader. In fact, if this is in a bash script, all you need to do is
Code:
kill 0 |
Quote:
But kill 0 is nice. I never though about it. |
Quote:
Code:
#!/bin/sh Code:
$ ./test.sh Code:
$ kill -- -$(pgrep test.sh) 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} Code:
kill 0 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 Code:
pkill -P $(pgrep test.sh) |
Quote:
Code:
$ kate & ta0kira |
Quote:
Xosen. |
All times are GMT -5. The time now is 04:14 AM. |