Solved

How do I kill a process and all its children?

Posted on 2001-06-07
14
556 Views
Last Modified: 2010-04-21
I have a GUI which forks a shell command
which in turn causes a series of forks
to happen so that I end up with a chain
of processes.  In the GUI I want to add
a button to kill the forked processes and
wish to program this easily.  So far the
only solution I can think of is to build up
a tree by walking the PPID PID fields returned
by ps starting with the PID of the original
forked process which I have and looking for
all PIDs that have that PID as their PPID
and then repeating this process recursively
until there are no more children to be found.
Once the tree is build I would then kill all
the processes in the tree with kill -9 pid for
each item in the tree.  This is rather tedious to
write so I am hoping to find a Unix command that
lets me kill a process and all of its children
processes transitively.
0
Comment
Question by:wkf
14 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 6166192
If you do a kill -9 `ps -ef | grep "<pid>" | awk '{ print $2 }'`, this will do it.  You can add some extra error checking (like using awk to tell if $2 (the process) or $3 (the parent) are the pid).  Note that the first is a backtick (shift+~), and the inner is a single quote.
I hope this helps a little.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 6166193
Correction, put spaces around the pid, and that will handle the $2 and $3 stuff I was talking about earlier:

kill -9 `ps -ef | grep " <pid> " | awk '{ print $2 }'`
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6166270
Hi wkf,

   use the following command:

   kill -9 `ps -ef | grep NAME | awk '{print $2}' `

   where: NAME is a program name you want to kill or the usr login
   name if you want to kill all the processes for that user.

   su as "root" before type in the above command, if you want to kill
other user's process.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 8

Expert Comment

by:mnashadka
ID: 6166339
yuzh, That's a good idea, but it will give undesirable results if you have more than one instance of that program running, though.  Of course, without knowing whether this is legal in wkf's environment, I can't say for sure that it's a problem.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6166369
If you have more than one instance of that program, and you only want  to kill one of them, all you need to do is:

    ps -ef | grep program | more

    find out which parent process ID and its child process you want to kill, then replace the NAME with the parent process ID, it'll kill all of them.
0
 
LVL 4

Expert Comment

by:Otetelisanu
ID: 6166574
In Solaris with
pkill

Make man pkill
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 6167377
yuzh, you can't do the more if you're trying to automate the kill in a program.  It requires user intervention at that point.  And what about the instance where you have a program called prog, somebody's looking at the log (tail -f prog.log), and there's another process (say a watchdog process to ensure that the daemon stays alive) called prog_watchdog.ksh.  Granted, prog is a bad name for a program, but you could plug in any other name and I've seen systems designed this way.  That's all I was saying.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6170301
Well, if you want to kill the daemon, that is easy. in a script situation, you can find the deamon PID first, then exclusive this ID from the kill list.

   deamon PID=`ps -ef | grep mydeamon | grep -v greg | awk '{print $2}'`

   from your killing list, you do:

   kill -9 `ps -ef | grep myprog | grep -v deamon-PID | awk '{print $2}'`


0
 
LVL 8

Expert Comment

by:mnashadka
ID: 6171072
Why would you exclude the daemon pid from your kill list if you want to kill the daemon?
0
 
LVL 1

Expert Comment

by:Haho
ID: 6175810
what about "killall processname"? would it help? :)
0
 
LVL 5

Expert Comment

by:Nisus091197
ID: 6176650
Hi,

Depending on how the children are forked, it is simply a matter of killing the parent nicely.

kill <PID>

Do not use '-9' as this will not give the parent a chance to kill its children and they will be adopted by init.

When the script is called capture the PID into a file.  Then let the kill button run this command:

kill `cat $PID_FILE`

where PID_FILE is an environment variable containing the PID.

This is all you need to do.

Regards, Nisus
http://www.omnimodo.com
0
 

Expert Comment

by:jf18222
ID: 6187882
Listen to Nisus above, DO NOT USE kill -9 unless the process will not die gracefully. Like he said, just kill the parent with a kill, this parent will kill the child processes before it dies.
0
 
LVL 1

Expert Comment

by:haeger
ID: 6517111
Hi.
I agree with "haho" above that 'killall' is a useful command, just don't use it on Solaris. It really kills ALL processes. It works fine on IRIX though, and will accept -9  (or -KILL) if it's nessecary.
0
 
LVL 3

Accepted Solution

by:
pjb1008 earned 300 total points
ID: 7249357
Make your GUI program run the children in their own process group. See setpgrp(2). Provided that the children don't try to change process group themselves, you'll be able to kill them by giving a negative argument to kill, ie.

kill -TERM -process_group_number



0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSH commands for Nas4free 21 461
Write an app 10 70
How do disable only TLSv1.0 in Oracle Sun One 7.1 Server 9 108
Shell Script on AIX 7 112
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question