Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the child pid from ppid 2

Status
Not open for further replies.

jbonin

Programmer
Mar 25, 2002
11
0
0
US
Does anyone know a way to get the child process id (PID)
from a given parent process id (PPID)?

Thanks,
John
 
note: replace [PID] with actual parent process id.

ps -ef|grep " [PID] "

if this pid shows up in the 3rd column,
then the second column is the child pid. Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Hello John,

I made this script on a HP machine.
Maybe your ps has different options.
The output is not yet exactly how I want it.
The output seems alright.

regards Gregor.



#!/usr/bin/sh
# Find all the child processes of a pid.
# Gregor Weertman

pid=$1
if [ -z "$1" ] ;then
echo child pid
exit
fi

for pid
do
ps -ef |awk -vpd=$pid '
function child( fpid, ff){
for( ii[ff] = 1; ii[ff] <= TNR; ii[ff]++){
if( ppid[ ii[ff]] == fpid) {
if( ppid[ ii[ ff]] == pd) {
print &quot;======\t====\t====\t====\t====\t====\t====&quot;
}
print psln[ ii[ff]]
pd1 = pid[ ii[ff]]
child( pd1, ff+1)
}
}
return 0
}
{
pid[ NR] = $2
ppid[ NR] = $3
psln[ NR] = $0
if( pid[ NR] == pd) {
print &quot;MAIN:&quot; psln[ NR]
pd1 = pid[ NR]
}
TNR = NR
}
END{
if( pd1 == 0) exit
child( pd1, 1)
}'
done
Gregor.Weertman@mailcity.com
 
Thanks Gregor, that looks interesting and I'll certainly try it out.

Another problem I'm trying to solve right now is another
post I made on the same day. I need to be able to trap
the message that the AT command sends to the terminal when
submitting a command to the AT utility queue.

You know the one that says something like...

Job user.xxxxxxxxx.a will be run at datetime

I tried different variations of setting output, but nothing
seems to work.

Thanks,
John
 
way to go Greg... Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Thanks, but that isn't what I was asking at all.
I wanted to capture the message that AT gives when you
run a command under AT.

I know how to do &quot;at- l&quot; to get a list. Unfortunately,
as part of &quot;at -l&quot; you don't get the actual name of
the shell that AT will be running, unless you have privileges to get to the cron/atjobs file.

What I need to do, without privileges, is to associate
the actual shell being submitted with the AT job name
you see with &quot;at -l&quot;. I just got it working, and what
I am doing is sending standard error to a file, then AT
writes the message with the job name to this file. I then
cut the AT job name from that file and create another temporary file, where I write the AT job name and the
actual shell name I had stored in a variable.

Problem solved, I can then read this file, present a
listing to the user with the name of the shell they
will recognize, and after they select the job to delete,
it will use the nonsense name given by AT to delete the job
from AT.

Thanks for the help anyway.

John
 
Excellent script, Gregor.
It finds all chld processes and grandchilden too!
You may want to consider adding a sort statment.
When I did this it was much easier to trace
the lineage when there were a lot of child processes.
Also, maybe add this script as a faq.


Robert Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Yes, definitely - turn in into a FAQ please Greg.
Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Robert,

I don't know what statement.
Can you give me a hind?

regards Gregor Gregor.Weertman@mailcity.com
 
It's going to take a while to digest your script
to tell you exactly how to do it.
But I just mean to sort the child pids that you find.
So the grandchildren appear below their parents
instead of the order being mixed up.

child [pid]|sort
works somewhat but, it also sorts the master PID
and the ===== etc.

Robert
Robert G. Jordan
Unix Sys Admin
Sleepy Hollow, Illinois U.S.A.

FREE Unix Scripts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top