LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-01-2006, 08:09 AM   #1
dalmassoc
LQ Newbie
 
Registered: Oct 2005
Location: france
Distribution: Fedora / Mandriva
Posts: 6

Rep: Reputation: 0
kill process and its child in a shell


Hi,
I look for a mean to kill a process and all its child in a shell script.

With the kill command I can do that:
kill -- -pid_process

that kill a process and all in the same group and its childs.
But that work only with job launch in interactive shell.

Any idea are mostly appreciate.

Cedric
 
Old 03-01-2006, 01:16 PM   #2
abhijeetudas
Member
 
Registered: Sep 2004
Distribution: Redhat / Fedora
Posts: 114

Rep: Reputation: 15
Since any way you are calling the function through a shell script you can write a few lines like

PPID=`pgrep <yourprogram>`
ps -ef | grep $PPID | awk ' { print $2 } '

This gives you a idea.

Havent had the time to check it out

but i think you get the idea.
 
Old 03-02-2006, 02:47 AM   #3
dalmassoc
LQ Newbie
 
Registered: Oct 2005
Location: france
Distribution: Fedora / Mandriva
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks for your idea.
I quickly do a sh function, maybe we can simplify it but it's work.


#print all the childs, grandson, great-grandson, ... pids
#warning not use it on your father because you create child and this do never return
get_child_pid_rec () {
local pids=

for pid in $*
do
local child_pids=
local child_pids_rec=

child_pids=$(ps -ef | awk -v ppid_p=$pid 'BEGIN { pids = "" } $3 == ppid_p { pids = pids $2 " " } END { print pids }');
if [[ $child_pids ]]
then
child_pids_rec=$(get_child_pid_rec $child_pids);
fi
pids="$pids $child_pids $child_pids_rec";
done
echo -n "$pids "
}
 
Old 03-02-2006, 08:18 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Can't you do the same by putting a "-" before the PID of the parent when kill'ing it?
Or does that only work for process groups?
 
Old 03-03-2006, 01:57 AM   #5
dalmassoc
LQ Newbie
 
Registered: Oct 2005
Location: france
Distribution: Fedora / Mandriva
Posts: 6

Original Poster
Rep: Reputation: 0
No as I say in the first post.

A "simple test" :
THE SCRIPTS:
user ~/tmp/testshell $ more lauch_main.sh
#!/bin/bash
./main.sh &
user ~/tmp/testshell $ more main.sh
#!/bin/bash
./prod.sh & # >&3 &
while :
do
echo "I am in the main"
sleep 10
done
user ~/tmp/testshell $ more prod.sh
#!/bin/sh
i=0
while :
do
echo $i
sleep 1
i=$(($i + 1))
done
user ~/tmp/testshell $

and THE EXECUTION:
user ~/tmp/testshell $ ./launch_main.sh
user ~/tmp/testshell $ ps -ef f | tail
ucdo8305 17849 17846 0 08:36 pts/5 S 0:00 \_ zsh
ucdo8305 17891 1 0 08:37 ? S 0:00 /usr/X11R6/bin/xterm.real -name Terminal -sb -sl 1500 -display 10.193.224.73:0.0
ucdo8305 17894 17891 0 08:37 pts/8 S 0:00 \_ zsh
ucdo8305 18242 17894 0 08:47 pts/8 R 0:00 \_ ps -ef f
ucdo8305 18243 17894 0 08:47 pts/8 S 0:00 \_ more
ucdo8305 18244 17894 0 08:47 pts/8 S 0:00 \_ tail
ucdo8305 18200 1 0 08:46 pts/5 S 0:00 /bin/bash ./main.sh
ucdo8305 18201 18200 0 08:46 pts/5 S 0:00 \_ /bin/sh ./prod.sh
ucdo8305 18241 18201 0 08:47 pts/5 S 0:00 | \_ sleep 1
ucdo8305 18227 18200 0 08:47 pts/5 S 0:00 \_ sleep 10
user ~/tmp/testshell $ ps -ef f| more | head -n1
UID PID PPID C STIME TTY STAT TIME CMD
user ~/tmp/testshell $ kill -- -18200
kill: kill -18200 failed: no such process
user ~/tmp/testshell $ kill 18200 18201
user ~/tmp/testshell $

but if you launch directly ./main.sh & and try to kill the group it works.
I don't know exactly why, IMO we may have a look on the shell (I use bash 3.xxx)

Regards
 
  


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
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
When I kill parent process, child isn't killed BengeBoy Linux - Software 0 08-19-2004 04:38 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 02:09 PM


All times are GMT -5. The time now is 01:24 PM.

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