LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Use only one "kill" to kill father and child processes (https://www.linuxquestions.org/questions/programming-9/use-only-one-kill-to-kill-father-and-child-processes-665753/)

Xosen 08-27-2008 07:04 AM

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.

Cuetzpallin 08-27-2008 08:31 AM

Quote:

Originally Posted by Xosen (Post 3261294)
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.

try killall {application_name}

Valery Reznic 08-27-2008 09:43 AM

Quote:

Originally Posted by Cuetzpallin (Post 3261419)
try killall {application_name}

You can run
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

osor 08-27-2008 01:36 PM

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
which should kill all processes with the same PGID (most likely all its children). Only those children which have deliberately joined a separate process group (through e.g., setsid) will not receive the signal.

Valery Reznic 08-27-2008 03:35 PM

Quote:

Originally Posted by osor (Post 3261744)
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
which should kill all processes with the same PGID (most likely all its children). Only those children which have deliberately joined a separate process group (through e.g., setsid) will not receive the signal.

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.

But kill 0 is nice. I never though about it.

osor 08-27-2008 04:51 PM

Quote:

Originally Posted by Valery Reznic (Post 3261900)
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,
Code:

$ ./test.sh
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
Code:

kill 0
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.

ta0kira 08-27-2008 05:05 PM

Quote:

Originally Posted by Valery Reznic (Post 3261506)
You can run
setsid <your_program>
setsid will create new process group

setsid actually prevents the creation of a new process group because it creates a new session. The process' group is implicitly it's own pid, though. setsid will also interfere with the way a shell normally shuts down its children. For example, try the following from a terminal emulator in KDE:
Code:

$ kate &
$ setsid kate

Next, close the terminal (which will signal the shell to terminate.) Notice which instance of kate is still open and which isn't.
ta0kira

Xosen 08-28-2008 03:33 AM

Quote:

Originally Posted by osor (Post 3261982)
If you want to kill all a script’s direct descendants, you can use pkill:
Code:

pkill -P $(pgrep test.sh)

I have done some test and this works the best. Thanks a lot for all the help.

Xosen.


All times are GMT -5. The time now is 04:14 AM.