Is there any cmd to kill a process including its childs ( or sub processes spawned by

Dear Unix Gurus,
Here is my query.
If i start a script,it inturn calls many other scripts ..and most of them continue to run in parallel.

Suppose,if i want to stop my script for some reason,i need to kill -9 each of the processes running.It becomes clumsy if the sub processes r more.

But,i feel should there b some way wherein if i kill my parent (initial) process..all the subs from it has 2 b killed.

Currently we r doing it in the below ways.

  • Kill -9 <all the processes> (need 2 copy&paste all the pids from ps -fu <uid> command)

-kill the shell/session - But this terminates the session/window.

I m thinking 2 write a small utility using awk 4 it..if any such cmd doesnt exist.
Before that,i just want make sure if there is any cmd 4 it r not.

Can any one of u throw some light on this thread.

Thanks in advance,
Venkat.

See the following use of pstree

[/tmp]$ cat 1.sh
#! /bin/sh 
echo ----
/tmp/"2.sh"
[/tmp]$ cat 2.sh
#! /bin/sh
/tmp/3.sh
sleep 10
[/tmp]$ cat 3.sh
#! /bin/sh
sleep 10
[/tmp]$ ./1.sh
[/tmp]$ ----
[/tmp]$ ps x
31599 pts/11   S      0:00 /bin/sh ./1.sh
31600 pts/11   S      0:00 /bin/sh /tmp/2.sh
31601 pts/11   S      0:00 /bin/sh /tmp/3.sh
31602 pts/11   S      0:00 sleep 10
31603 pts/15   R      0:00 ps x
[~]$ pstree -p 31599
1.sh(31599)---2.sh(31600)---3.sh(31601)---sleep(31602)
[~]$ 

Vino

Vino,
Thx for the quick reply.
pstree lists all the pids of sub processes with pnames.

anyhow,we need 2 again manually pick-up those pids and kill.right?

after pstree...we can procees that list usoing any of the utilities and pick only the pids and pipe the 2 xargs.

I think this way ll b fine..right?

or pls suggest me if there is a better way.

Thanks,
Venkat (BEA Systems).

After you get the pids from pstree, you dont need xargs.

You can do

kill -9 31599 31600 31601 31602

Let me make it clear.

I want 2 avoid manullly picking up those ids and killing.

if its less,manual is ok.

but is its more (say > 20), it takes bit time time.right?

instead we can process the o/p pf pstree and extract only pids (omit pnames)...then do xargs kill on the list.

Does that makes sense?

Pls let me know..

Thanks,
Venkat @ BEA.

check whether this works ?

ptree pid |sed 's/^[ ]*\(.\) \/./\1/' | xargs kill

#! /bin/ksh

PSTREE="1.sh(31599)---2.sh(31600)---3.sh(31601)---sleep(31602)"

PIDS=$(echo "$PSTREE" | grep -o '[0-9]\{2,5\}')

for pid in "$PIDS"
do
# Subsitute kill -0 with kill -9
kill -0 $pid
done

Vino,
Ur command simply worked well..
Thanks a lot.

I just put in a simple way.

pstree -p <pid>|grep -o '[0-9]\{2,5\}' | xargs kill -9

(pid : pid of ur start script)

u mentioned\{2,5\}..this works for pid ranges of 2-5 digits.

i dont know exactly the max pid value ..is it the max int value?
and the least pid has it b atleast 2 digits?
otherwise we can,simply make it \{1,5\}.

Pls let me know whether my assumption is correct..with respect to \{2,5}\.

Thanks a lot again.

Mahender,
Thanks for ur reply too..i am just analyzing ur reply.

Thanks Friends,
Venkat @BEA.

Go ahead with \{1,\}

That should work well.

Thx Vino..

The only problem I see with \{1,\} is that if a script has the a numeral in its name, then it would pick out that number and kill a process corresponding to that number. That pid would not be corresponding to that script at all.

Vino,
Then we ll filter it out using 1 more grep level as below.

pstree -p 3887 | grep -o '([0-9]*)'|grep -o '[0-9]\{1,5\}'|xargs kill -9

I think above one should be ok to filter out that scenario.

Thanks,
Venkat @BEA.

You might want to look at this old thread as well - Script to kill all child process for a given PID

Thanks Vino..

Could u pls give me ur mail-id (either office / personel).

I didnt google around for long. But I saw this in one of Oreillys' book - Practical UNIX & Internet Secuirty

The max value is 65535. The least can be 1. But those are the initial processes that run. I doubt you want to include them in your search.

If you have any questions, I'd suggest you post them in the forum itself.

Rules say

(10) Don't post your email address and ask for an email reply. The forums are for the benefit of all, so all Q&A should take place in the forums.

Though, I am at the other end of the above mentioned spectrum, it still applies.

If you have a non-technical question for me, use the forum's private messaging system.