×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

UNIX Scripting FAQ

How do I get all the processes related to a PID ? by PHV
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 UNIX Scripting FAQ Index
Back to UNIX Scripting 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