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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there any cmd to kill a process including its childs ( or sub processes spawned by
# 1  
Old 12-23-2005
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.
# 2  
Old 12-23-2005
See the following use of pstree

Code:
[/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]$ ----

Code:
[/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
# 3  
Old 12-23-2005
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).
# 4  
Old 12-23-2005
After you get the pids from pstree, you dont need xargs.

You can do

Code:
kill -9 31599 31600 31601 31602

# 5  
Old 12-23-2005
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.
# 6  
Old 12-23-2005
check whether this works ?

ptree pid |sed 's/^[ ]*\(.*\) \/.*/\1/' | xargs kill
# 7  
Old 12-23-2005
Code:
#! /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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

Good afternoon I need to KILL a process in a single command sentence, for example: kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'` That sentence Kills the process ID corresponding to the program CAL255.4ge. However it is possible that the same program... (6 Replies)
Discussion started by: enriquegm82
6 Replies

2. Shell Programming and Scripting

Process behavior different when spawned from single terminal

So this one just plain confuses me. I have a bunch of somewhat CPU intensive processes that all communicate using a shared memory region. Some of these programs are threaded and some also change the scheduling to FIFO or round robin. The good news is that everything works as long as I spawn... (3 Replies)
Discussion started by: talkingfennel
3 Replies

3. Shell Programming and Scripting

Preparing a list of spawned processes

Hi All I'm currently trying to develop a script which will find the child processes of a process ID already passed to the script. I then need the script to look for spawned processes of these child processes and so on until it can't find any more. For example At the moment, I have to... (6 Replies)
Discussion started by: huskie69
6 Replies

4. Shell Programming and Scripting

Suppressing output of a spawned telnet process

Hi, I'm trying to spawn a telnet process and trying to do some actions in the remote host using expect script. I would like to know how to suppress all the output in order the user using the script should not be able to see any actions done on the remote host. I tried using the "log_user 0"... (8 Replies)
Discussion started by: arun_maffy
8 Replies

5. UNIX for Dummies Questions & Answers

Finding apache process id that is spawned by a wget

Hi, I have a situation where I am writing a programme that runs a series of long running PHP scripts that can take anything from 20 minutes to 10 hours to execute. I have a solution half implemented where I use via php exec(wget <location to command>) and get the process id back. This... (1 Reply)
Discussion started by: mrploddy
1 Replies

6. UNIX for Dummies Questions & Answers

kill/pkill process by CMD info.

I have a process that I'd like to kill. Doing a "ps -fu myusername" gives me: UID PID PPID C STIME TTY TIME CMD myusername 5443 1 0 10:05 ? 00:00:00 /bin/sh /some/path/crap.sh -s /yet/another/path/parentProcess myusername 5593 5443 0 ... (2 Replies)
Discussion started by: mrwatkin
2 Replies

7. Linux

How to receive results from processes spawned on external machines using SSH

I am trying to get the number of cpus on a farm of linux boxes (about 100 of them) by 'sshing' to each of them and checking their /proc/cpuinfo file. So I have a local script localscript.sh on each of those 100 machines which retrieves the number of cpus in it by using its /proc/cpuinfo file.... (1 Reply)
Discussion started by: waavman
1 Replies

8. Shell Programming and Scripting

How to kill a process and its childs given pid and userid

I need to write a shell script which would take 2 arguments pid , userid. Then it should kill all the child process under it. If a child process is not killed then it should wait for 1 minute and should kill. can anybody give me the idea to write it? (0 Replies)
Discussion started by: nani_g
0 Replies

9. UNIX for Advanced & Expert Users

When kill doesnt work, how to kill a process ?

Hi All, I am unable to kill a process using kill command. I am using HP-UX system. I have tried with kill -9 and i have root privilages. How can i terminate this daemon ? ? ? Regards, Vijay Hegde (3 Replies)
Discussion started by: VijayHegde
3 Replies

10. UNIX for Dummies Questions & Answers

How to use Kill cmd when detect rogue

I'm system administrator and most of our Unix servers in the company host database that are accessed frequently by company employees. One day, one particular Unix server has been reported as being very slow. Upon further investigation using the ps command, we've found a rogue process that is... (8 Replies)
Discussion started by: agui
8 Replies
Login or Register to Ask a Question