|
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!
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? Subscribe!
"--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 :/ Posted by: monsieur at May 17, 2006 8:38 AMhow to list all process having PPID as Null??? Posted by: kutta Kumar at May 22, 2006 9:45 PMOther'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:
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 Posted by: pushpakanth at May 18, 2007 9:51 PMpkill easily kills the parent and all children: pkill -P "$PPID" Posted by: dan at July 11, 2007 3:44 PMI 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. Posted by: sidailurch at January 20, 2008 2:58 AMsidailurch, 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 AMYou can use this script kill your child httpd pids with out killing parent! ------------------- Enjoy!!!! Posted by: Nishad Nanethan Aliyar at August 15, 2009 2:55 AMcan you elaborate this topic in more detail,so i can understand it a little bit.thanks... Posted by: shital at September 14, 2010 3:36 AMI have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
Recommended
Recent Entries
Search
I Need Help!
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and RSS Feeds Building Web Site Traffic Business and Management CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above Facebook Help Google Plus Help HTML and CSS Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Mac OS X Help Pay Per Click (PPC) Advertising 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 WordPress Help |