1: #!/bin/sh 2: # 3: # Original code, with very minor changes needed to run on new OS 4: # versions. RB Oct 2002 5: #--------------------------------------------------------------- 6: # 7: # Copyright 1995, by Hewlett-Packard Company 8: # 9: # The code in this file is from the book "Shell Programming 10: # Examples" by Bruce Blinn, published by Prentice Hall. 11: # This file may be copied free of charge for personal, 12: # non-commercial use provided that this notice appears in 13: # all copies of the file. There is no warranty, either 14: # expressed or implied, supplied with this code. 15: # 16: # NAME 17: # ptree - print a process tree 18: # 19: # SYNOPSIS 20: # ptree [-n] [pid] 21: # ptree [-n] [pid] [level] [datafile] 22: # 23: # DESCRIPTION 24: # This command will print the process tree for the 25: # process identified by pid. When called by the user, 26: # only the first format shown should be used. If the 27: # process ID (pid) is not passed, process 1 is assumed. 28: # 29: # The second format of this command is only used when 30: # this command calls itself recursively to print the next 31: # level of the process tree. 32: # 33: # -n Do not recurse 34: # 35: # RETURN VALUE 36: # 0 Successful completion 37: # 1 Usage error 38: # 39: ############################################################ 40: # PATH=$PATH:`dirname $0` 41: ## Make sure /usr/bin is ahead of any other possible versions of ps 42: PATH=/usr/bin:$PATH:`dirname $0` 43: 44: . SystemType.sh 45: 46: CMDNAME=`basename $0` 47: USAGE="Usage: $CMDNAME [-n] [pid]" 48: RECURSIVE=TRUE # List processes recursively 49: PROCESS= # PID of the starting process 50: LEVEL= # Indentation level (num parents) 51: DATAFILE= # Reformatted output from ps 52: PSFILE=/tmp/ps.$$ # Output from the ps command 53: PS_OPTS= # Options for the ps command 54: OLD_IFS=$IFS # Original value of IFS variable 55: SYSTEM=`SystemType` # String identifying the system 56: 57: # 58: # Temporaries 59: # 60: PID= # Process ID 61: PPID= # Process ID of the parent process 62: OWNER= # Owner of the process 63: NAME= # Name of the process 64: LINE= # Line from the data file 65: OUTLINE= # Line of output 66: INDENT= # Column number where line begins 67: 68: trap 'rm -f /tmp/*.$$; exit 1' 1 2 3 15 69: 70: FillLine() { 71: # 72: # SYNOPSIS 73: # FillLine line column 74: # 75: _LINE="$1" 76: _COLUMN=$2 77: _LEN=`expr "$_LINE" : '.*'` 78: while [ $_LEN -lt $_COLUMN ] 79: do 80: _LINE="$_LINE " 81: _COLUMN=`expr $_COLUMN - 1` 82: done 83: 84: echo "$_LINE" 85: } 86: 87: while : 88: do 89: case $1 in 90: -n) RECURSIVE=FALSE 91: shift 92: ;; 93: --) shift 94: break 95: ;; 96: -*) echo "$USAGE" 1>&2 97: exit 1 98: ;; 99: *) break 100: ;; 101: esac 102: done 103: 104: # 105: # Make sure the number of parameters is reasonable. 106: # 107: if [ $# -eq 0 ]; then 108: PROCESS=1 109: LEVEL=0 110: DATAFILE=/tmp/ptree.$$ 111: elif [ $# -eq 1 ]; then 112: PROCESS=$1 113: LEVEL=0 114: DATAFILE=/tmp/ptree.$$ 115: elif [ $# -eq 3 ]; then 116: PROCESS=$1 117: LEVEL=$2 118: DATAFILE=$3 119: else 120: echo "$USAGE" 1>&2 121: exit 1 122: fi 123: 124: if [ "$LEVEL" = 0 ]; then 125: # 126: # Determine which options to use with the ps command. 127: # 128: case $SYSTEM in 129: SUNBSD | ULTRIX ) PS_OPTS="-auwx" ;; 130: * ) PS_OPTS="-ef" ;; 131: esac 132: 133: # 134: # Build the data file. 135: # 136: rm -f $DATAFILE $PSFILE 137: ps $PS_OPTS | sed '1d' | sort >$PSFILE 138: 139: exec <$PSFILE 140: IFS= 141: while read LINE 142: do 143: IFS=$OLD_IFS 144: 145: set $LINE 146: OWNER=$1 147: PID=$2 148: PPID=$3 149: 150: # 151: # Determine column where the process name begins. 152: # 153: case $SYSTEM in 154: SGI62 ) COL=47 ;; 155: AIX | HP | SGI | SOLARIS ) COL=48 ;; 156: SUNBSD | DECOSF | SGI65 ) COL=57 ;; 157: ULTRIX ) COL=51 ;; 158: * ) echo "Unexpected system type." 1>&2 159: exit 1 160: ;; 161: esac 162: 163: LINE=`echo "$LINE" | cut -c$COL-` 164: set dummy $LINE 165: shift 166: NAME=$1 167: 168: echo $PID $PPID $OWNER $NAME >>$DATAFILE 169: IFS= 170: done 171: IFS=$OLD_IFS 172: fi 173: 174: # 175: # Print the current process. 176: # 177: INDENT=`expr $LEVEL \* 2` 178: OUTLINE=`FillLine "" $INDENT` 179: 180: LINE=`grep "^$PROCESS " $DATAFILE` 181: set $LINE 182: OUTLINE="$OUTLINE $1" 183: OUTLINE=`FillLine "$OUTLINE" 30` 184: OUTLINE="$OUTLINE $3 $4" 185: echo "$OUTLINE" 186: 187: if [ "$RECURSIVE" = "TRUE" ];then 188: LEVEL=`expr $LEVEL + 1` 189: while read LINE 190: do 191: set $LINE 192: # 193: # For every process that is a child of the 194: # current process, invoke this command ($0) 195: # recursively. 196: # 197: if [ "$2" = "$PROCESS" ]; then 198: $0 $1 $LEVEL $DATAFILE 199: fi 200: done <$DATAFILE 201: fi 202: 203: rm -f /tmp/*.$$ 204: exit 0
last modified 22/03/2012 | Introduction | Table of Contents (frame/no frame) |
Printable (single file) |
© Dartmouth College |