How do I kill a process and all its children?

Posted on 2001-06-07
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.
Question by:wkf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

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.

Expert Comment

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 }'`
LVL 38

Expert Comment

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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

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.
LVL 38

Expert Comment

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.

Expert Comment

ID: 6166574
In Solaris with

Make man pkill

Expert Comment

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.
LVL 38

Expert Comment

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}'`


Expert Comment

ID: 6171072
Why would you exclude the daemon pid from your kill list if you want to kill the daemon?

Expert Comment

ID: 6175810
what about "killall processname"? would it help? :)

Expert Comment

ID: 6176650

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

Expert Comment

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.

Expert Comment

ID: 6517111
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.

Accepted Solution

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


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
CPU monthly average? 12 150
NTP Server in VMware 5 466
unix solaris snoop command 6 139
Log File Creation with Header and Footer 17 187
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap ( Version 1.2 2.      Jpcap( Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

751 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