Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


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!


More Useful Unix and Linux Help Articles:
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔   Can I script renaming files based on an XML data map?
I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔   Test for valid numbers in a Bash shell script?
In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔   Review: iSSH for the iPad/iPhone
If you're running an online business like I am, there are times when you need to connect and log in to the server...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  









Reader Comments To Date: 10

kjteoh said, on February 7, 2006 4:51 AM:

pstree will do the trick.

kjteoh

monsieur said, on May 17, 2006 8:38 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 :/

kutta Kumar said, on May 22, 2006 9:45 PM:

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

Jeff Breadner said, on April 3, 2007 1:03 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
}

pushpakanth said, on May 18, 2007 9:51 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

dan said, on July 11, 2007 3:44 PM:

pkill easily kills the parent and all children:

pkill -P "$PPID"

sidailurch said, on January 20, 2008 2:58 AM:

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.

Dave Taylor said, on February 4, 2008 8:46 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.

Nishad Nanethan Aliyar said, on August 15, 2009 2:55 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!!!!

shital said, on September 14, 2010 3:36 AM:

can you elaborate this topic in more detail,so i can understand it a little bit.thanks...

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

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











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!
Powered By
Linux Journal: Free Issue!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 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. Further, 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. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.