Solved

How do I kill a process and all its children?

Posted on 2001-06-07
14
547 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
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
Comment Utility
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
Comment Utility
In Solaris with
pkill

Make man pkill
0
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 38

Expert Comment

by:yuzh
Comment Utility
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
Comment Utility
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
Comment Utility
what about "killall processname"? would it help? :)
0
 
LVL 5

Expert Comment

by:Nisus091197
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now