Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I'm a freelance consultant, and your site's helped me with many issues. I just wanted you to know that someone does appreciate the intelligent help your site offers."

Geography

Where in the world do Tek-Tips members come from?

Tips and Tricks

How do I get all the processes related to a PID ?
Posted: 17 Dec 03

Here a sample script that output all the parent processes, the related process and all the child processes of a given PID:
ps -fe | awk -v pid=$1 '
BEGIN{if(!pid){
  t="/dev/tty";printf "PID ? ">t;getline<t;pid+=$1
}}
function child(id  ,i){
 print line[id];if(id==pid)printf "\n"
 for(i in ppid) if(ppid[i]==id) child(i)
}
function parent(id  ,i){
 i=ppid[id]
 if(line[i]>"" && i!=id) buf=parent(i)"\n"line[i]
 return buf
}
$2==pid{++found}
{ppid[$2]=$3;line[$2]=$0}
END{if(found){
  print line["PID"]"\n"parent(pid)"\n";child(pid)
}}
'

If you're interested only by child processes, replace the print statement in the END section with this:
  print line["PID"];child(pid)

If you're interested only by parent processes, replace the print statement in the END section with this:
  print line["PID"]parent(pid)"\n"line[pid]

Back to -General UNIX discussion FAQ Index
Back to -General UNIX discussion Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close