Friday, October 14, 2005

[UNIX] Process Tree

#! /bin/sh

# List all processes in tree format (show dependency)

this=$$
ps -ef | awk -v this=$pid '
 function ptree(myid,space,  lpid) {
  split(p2c[myid],lpid,",")
  for(i in lpid) {
   id=lpid[i]
   print space, id, cmd[id]
   spc=sprintf("%s%s",space,"  ")
   ptree(id,spc);
  }
 }

        NR>1 {
  n=length($0); process=substr($0,48,n-48+1)
                pid=$2
                ppid=$3
  if ( pid == this || ppid == this ) {
   next
  }
                c2p[$2]=$3
  cmd[$2]=process
  if ( length(p2c[$3]) == 0 ) {
   p2c[$3]=$2
  } else {
                 p2c[$3]=sprintf("%s,%s",p2c[$3],$2)
  }
 } 
 END {
  ptree(1,"")
 }'

0 Comments:

Post a Comment

<< Home