Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I kill a process and all its children?

Posted on 2001-06-07
14
Medium Priority
?
576 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
[X]
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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 600 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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 get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

705 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