The script (or most of it):
AWK=nawk
PS="ps -eaf -o pid,ppid,user,args"
#-----------------------------------------------------------
#Function to find the child processes and display them
#-----------------------------------------------------------
function childpid {
set -A childarray #Null the array
pid=$1
processes=`eval $PS | grep $pid | grep -v $$ | $AWK '{ if ($2 == p) print }' p=$pid` # if the 2ndd field = pid then print the ps
if [ "$processes" ] # if processes is not null
then
echo "$processes" | while read process # echo the process to the screen and read each line
do
child1=`echo "$process" | $AWK '{ print $1 }'` # print the first field
child2=`echo "$process" | $AWK '{ print $4 }'` # print the fourth field
echo "\t\t$child1\t\t $child2" # print the child process
pid=`echo "$process" | $AWK '{ print $1 }'`
#echo "$ARR2"
childarray[$ARR2]=$pid # NO spaces
#echo $pid # for debug
#echo "${childarray[$ARR2]} Array number" # for debug
ARR2=$(($ARR2+1))
childpid $pid # find the child of the process
done
fi
}
#-----------------------------------------------------------
#Trap any errors
#-----------------------------------------------------------
trap "echo 'Control-C or Control-D cannot be used' ; sleep 1 ; clear ; continue " 1 2 3
while true
do
clear
set -A pidarray
set -A childarray
ARR=0
pid=0
#-----------------------------------------------------------
#Load the PID numbers from the file into an array
#-----------------------------------------------------------
set -A pidarray #Null the array
cat $DIRTEMP$PIDFILE | while read LINE #read each line in the file
do
pidarray[$ARR]=$LINE # NO spaces
#echo $LINE # for debug
#echo "${pidarray[$ARR]} Array number" # for debug
ARR=$(($ARR+1))
done
#-----------------------------------------------------------
#First Menu of Choices
#-----------------------------------------------------------
echo "TRW Automotive Aftermarket: `hostname`\t\t User:$FULLNAME\n"
echo "\t MLE UTILITIES MENU - KILL MLE SCRIPTS - $SYSTEM
\t ------------------------------------"
NUMPIDS=0
i=0
processes=""
NUMPIDS=${#pidarray[*]} #count the number of arrays
#echo "$NUMPIDS Number of arrays" # for debug
echo "\t \t`ps -p 999999999999999999 -o pid,user,time,args`\n" # set up header
while (( i < ${NUMPIDS} )) #while the array count < i
do
pid=${pidarray}
#echo "$pid" #for debug
echo "\t $i -- \t`ps -p $pid -o pid,user,time,args | tail -1`"
processes="start"
while [ "$processes" ]
do
echo "\t\t------------------ child processes ------------------"
childpid $pid # find the child processes
done
echo "\n"
((i+=1))
done