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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get all the processes related to a PID ?

How do I get all the processes related to a PID ?

by  PHV  Posted    (Edited  )
Here a sample script that output all the parent processes, the related process and all the child processes of a given PID:
Code:
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:
Code:
  print line["PID"];child(pid)
If you're interested only by parent processes, replace the print statement in the END section with this:
Code:
  print line["PID"]parent(pid)"\n"line[pid]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top