LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-27-2008, 07:04 AM   #1
Xosen
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Rep: Reputation: 0
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.
 
Old 08-27-2008, 08:31 AM   #2
Cuetzpallin
Member
 
Registered: Feb 2008
Location: Monterrey, MX
Distribution: Slackware since 3.4 and love it!!!
Posts: 162

Rep: Reputation: 31
Quote:
Originally Posted by Xosen View Post
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}
 
Old 08-27-2008, 09:43 AM   #3
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 666

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by Cuetzpallin View Post
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
 
Old 08-27-2008, 01:36 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
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.
 
Old 08-27-2008, 03:35 PM   #5
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 666

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by osor View Post
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.
 
Old 08-27-2008, 04:51 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by Valery Reznic View Post
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.
 
Old 08-27-2008, 05:05 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Valery Reznic View Post
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

Last edited by ta0kira; 08-27-2008 at 05:06 PM.
 
Old 08-28-2008, 03:33 AM   #8
Xosen
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by osor View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explained: "kernel panic - not syncing - attempted to kill init" sundialsvcs Linux - Software 36 04-24-2010 08:58 AM
suse 10.0 => processes "sending kill signal": "done, done, missing"...!? ungua Linux - Newbie 2 01-18-2006 10:52 AM
how to use kill to kill a batch of processes with same name? dr_zayus69 Linux - Software 2 09-03-2005 06:35 PM
Will "grub-install /dev/hda" kill Windows XP MBR? Zullocrit Linux - Newbie 3 09-25-2004 09:50 AM
"Attempted to kill idle/init task" during installation of Mandrake 10 e.fabene Mandriva 0 06-08-2004 10:01 AM


All times are GMT -5. The time now is 07:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration