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 | \
awk "{ if ( \$2 == $pid ) { print \$1 }}")
do
echo "Killing child process $child because ppid = $pid"
kill $child
done
That should do what you seek!
can you elaborate this topic in more detail,so i can understand it a little bit.thanks…
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!!!!
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.
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.
pkill easily kills the parent and all children:
pkill -P “$PPID”
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
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
}
how to list all process having PPID as Null???
“–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 :/
pstree will do the trick.
kjteoh
Lots of new articles from Ask Dave Taylor!
It’s never quiet at my Ask Dave Taylor tech support site, and it’s just about time for a roundup of the latest articles on a wide range of topics, from Sony PSP to Windows XP, eBay payment fraud to search engine optimization. Here’s the full rundown: &…