5 # phier: Show process tree incorporating a process.
6 # Notes: phier functions similarly (but not identically) to Solaris ptree.
7 # Usage: phier [-s <search>] <pid>
8 # Usage: phier [-s <search>] [-u <user>] [-x] <process>
9 # Options: -s <search> Stop hierarchy at this process name.
10 # -u <user> Start search from processes owned by user.
11 # -x Match process name exactly.
12 # Example: phier -s sshd $$
13 # Example: phier -s sshd -u iain nxagent
16 function find_parent() {
20 if [ -z "$child" ]; then
21 echo >&2 "No child to find."
25 cmd=$(ps -o comm= -p $child)
26 pid=$(ps -o pid= -p $child)
27 ppid=$(ps -o ppid= -p $child)
29 # echo >&2 "Finding from $child ($top) $pid/$ppid/$cmd"
31 if [ -z "$pid" ]; then
32 echo >&2 "Can't find PID $child!"
36 if [ ! -z "$top" ]; then
37 if [ "$cmd" = "$top" ]; then
43 if [ $pid = 1 -o $ppid = 1 ]; then
48 find_parent $ppid $top
56 while getopts ":s:u:x" opt; do
59 u) pgrep_opts="$pgrep_opts -u $OPTARG";;
60 x) pgrep_opts="$pgrep_opts -x"
66 echo >&2 "phier: Print process hierarchy for a process."
67 echo >&2 "Usage: phier [-s <search>] <pid>"
68 echo >&2 "Usage: phier [-s <search>] [-u <user>] [-x] <process>"
69 echo >&2 "Options: -s <search> Stop hierarchy at this process name."
70 echo >&2 " -u <user> Start search from processes owned by user."
71 echo >&2 " -x Match process name exactly."
72 echo >&2 "Example: phier -s sshd $$"
73 echo >&2 "Example: phier -s sshd -u iain nxagent"
78 if ! echo "$children" | grep -qs '^[0-9]*$'; then
79 children=$(pgrep $pgrep_opts "$children")
83 for child in $children; do
84 parent=$(find_parent "$child" "$top")