Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

How do I find all child processes in Unix?

There is a process that must be shutdown for maintenance purpose every morning at 5:00 am. Usually it spawn several children and sometimes some of those children don't die, so the parent process won't die either. When the application starts up again it raises a new parent process but in confict with the old one that hasn't died yet. All I need is kill the childs of the old process.

How can I look for every process when I know the parent process id?


Dave's Answer:

First off, I have to say that it sounds like the application isn't too well written if you're seeing this sort of process conflict, but that's the pragmatic reality of life on servers, I think.

Fortunately, the solution is fairly straightforward.

First off, you can find the process ID (PID) of the parent process by using a sequence of ps -aux | grep pattern. If your life is going to be easy, there'll always only be one match, but if there are also child processes, it's a bit more tricky.

In general, modern versions of ps let you specify exactly what fields you want to have output with the '-o' flag, so here's how I'd approach that for a process called "httpsd":

$ $ ps -o user,pid,ppid,command -ax | grep httpsd
root   47248     1 /usr/local/apache/bin/httpsd
www    47249 47248 /usr/local/apache/bin/httpsd
www    47250 47248 /usr/local/apache/bin/httpsd
www    47251 47248 /usr/local/apache/bin/httpsd
www    47252 47248 /usr/local/apache/bin/httpsd
www    47253 47248 /usr/local/apache/bin/httpsd
www    92713 47248 /usr/local/apache/bin/httpsd

In this output you can see that the first column shows the userID, the second shows the PID, and the third the PPID. Notice that the parent process not only has PPID of 1, but also is the only one running as root. This makes it easy to mask that one out and identify all the processes that are spawned by the "httpsd" program:

ps -o user,pid,ppid,command -ax | grep httpd | grep -v root

But that's not exactly what you're asking. Instead, you're asking me how to identify processes based on their PPID. To do that, let's drop the PID of the parent into a variable first:

pid=$(ps -o user,pid,ppid,command -ax | grep httpd | \
  grep root | awk '{print $2}')

Now I have a variable "pid" that has the process ID of the parent application. To find all the child apps, I just search for those that have that particular PID as their PPID.

That's actually just a slightly more complex awk script needed, as demonstrated in this snippet:

for child in $(ps -o pid,ppid -ax | \
  awk "{ if ( \$2 == $pid ) { print \$1 }}")
do
echo "Killing child process $child because ppid = $pid"
kill $child
done

That should do what you seek!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

pstree will do the trick.

kjteoh

Posted by: kjteoh at February 7, 2006 4:51 AM

"--ppid pidlist Select by parent process ID. This selects the processes with a parent process ID in pidlist. That is, it selects processes that are children
of those listed in pidlist."

from man ps :/

Posted by: monsieur at May 17, 2006 8:38 AM

how to list all process having PPID as Null???

Posted by: kutta Kumar at May 22, 2006 9:45 PM

Other's comments about things like pstree and ps --ppid aren't as reliable in multi-platform environments, I like Dave's solution as it will work pretty much anywhere.

However, it doesn't find the children of the child processes, it only goes one level deep. This quick tweak to Dave's script will recursively find all children:


find_children() {
for pid in $(ps -ef | awk "{if ( \$3 == $1 ) { print \$2 }}")
do
echo $pid
find_children $pid
done
}

Posted by: Jeff Breadner at April 3, 2007 1:03 PM

hi
can you write a bash script for following question

List all processes of a particular user (e.g. user root) and extract the program names of those processes and the time the processes started

Posted by: pushpakanth at May 18, 2007 9:51 PM

pkill easily kills the parent and all children:

pkill -P "$PPID"

Posted by: dan at July 11, 2007 3:44 PM

I know this is a really old post, but I can't get that to work, I keep getting:

awk: cmd. line:1: { if ( $2 == 1
awk: cmd. line:1: ^ unexpected newline or end of string

I'm sure i'm doing something stupid, i always run into problems with sed and awk.

Posted by: sidailurch at January 20, 2008 2:58 AM

sidailurch, I'm wondering if you aren't putting the quotes in the proper place? Try typing in the script rather than copying and pasting it to ensure that you aren't getting into a problem due to character set or character encoding issues too.

Posted by: Dave Taylor at February 4, 2008 8:46 AM

You can use this script kill your child httpd pids with out killing parent!

-------------------
kill -9 `ps -o pid,ppid,cmd -ax | grep httpd | awk '{ if($2!=1){print $1}}'`
-------------------

Enjoy!!!!

Posted by: Nishad Nanethan Aliyar at August 15, 2009 2:55 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Linux Journal: Free Issue!

Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.