SUMMARY 2: C code to find all descendants of a given PID

From: lombardi emanuele <lele_at_mantegna.casaccia.enea.it>
Date: Wed, 09 Dec 1998 11:51:46 +0100 (MET)

Lucio Chiappetti <lucio_at_ifctr.mi.cnr.it> suggested to make a ps filter
to produce the "family tree" of a given process. Now he sent me a csh
script which actually does that.
I'm glad to share with you this nice & usefull code.

Thank you lucio (ciao. ho leggermente modificato lo script cosi' lo
posso chiamare come voglio. Ho aggiunto il setenv iniziale perche' non
tutti lo hanno gia' settato. Grazie dell' aiuto.)

Emanuele


-- 
 Emanuele Lombardi
 mail:  AMB-GEM-CLIM ENEA Casaccia
        I-00060 S.M. di Galeria (RM)  ITALY
 mailto:lele_at_mantegna.casaccia.enea.it
 tel	+39 6 30483366 fax	+39 6 30483591
     This transmission was made possible by 100% recycled electrons.
#!/bin/csh
setenv CMD_ENV bsd   #for /usr/bin/echo -n to work
#
# print tree of PIDs under a given pid
#
set thisscript=$0
# may be called recursively, if not use level 0 (from command line)
  if ($#argv != 2) then
     set level = 0
  else
     set level = $2
  endif
#
# does wished process exist ?
  set pid = `ps -A -o user,pid,ppid,ucomm | awk -v mypid=$1 -v thispid=$$ '$2==mypid && $2!=thispid {print $1,$4}'`
  if ($#pid == 0) then
    echo "no such process"
    exit
  endif
#
# it does exist, print a number of tabs equal to the level minus one
  if ($level != 0) then
     set i = 0
loop:
     _at_ i++
     /usr/bin/echo -n "\t"
     if ($i != $level) goto loop
  endif
#
# echo my name and pid
  echo $1 $pid
#
# get any sons (ignore the instances of this program)
  set sons = `ps -A -o user,pid,ppid,ucomm | awk -v mypid=$1 '$3==mypid && $4!="$thisscript" {print $2}'`
#
# no sons
  if ($#sons == 0) then
#    echo "no sons"
     exit
  endif
#
# increment level by one
  _at_ nlevel = $level + 1
# and call recursively for each son
  foreach i ($sons)
     $thisscript $i $nlevel
  end
Received on Wed Dec 09 1998 - 10:54:28 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:38 NZDT