|
|
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? 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 | \ 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/iPhoneIf 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!
Categorized:
Unix and Linux Help
(Article 4715,
Written by Dave Taylor)
Tagged: Previous: Convert WMA from Windows Media Player into MP3 files? Next: What's the performance trade-off with partitioning a SATA drive? Reader Comments To Date: 10monsieur 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 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:
pushpakanth said, on May 18, 2007 9:51 PM:
hi 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 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! ------------------- 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...
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
pstree will do the trick.
kjteoh