3 # phier: Show process tree incorporating a process.
4 # Notes: phier functions similarly (but not identically) to Solaris ptree.
5 # Usage: phier [-s <search>] <pid>
6 # Usage: phier [-s <search>] [-u <user>] [-x] <process>
7 # Options: -s <search> Stop hierarchy at this process name.
8 # -u <user> Start search from processes owned by user.
9 # -x Match process name exactly.
10 # Example: phier -s sshd $$
11 # Example: phier -s sshd -u iain nxagent
14 function find_parent() {
18 if [ -z "$child" ]; then
19 echo >&2 "No child to find."
23 cmd=$(ps -o comm= -p $child)
24 pid=$(ps -o pid= -p $child)
25 ppid=$(ps -o ppid= -p $child)
27 # echo >&2 "Finding from $child ($top) $pid/$ppid/$cmd"
29 if [ -z "$pid" ]; then
30 echo >&2 "Can't find PID $child!"
34 if [ ! -z "$top" ]; then
35 if [ "$cmd" = "$top" ]; then
41 if [ $pid = 1 -o $ppid = 1 ]; then
46 find_parent $ppid $top
54 while getopts ":s:u:x" opt; do
57 u) pgrep_opts="$pgrep_opts -u $OPTARG";;
58 x) pgrep_opts="$pgrep_opts -x"
64 echo >&2 "phier: Print process hierarchy for a process."
65 echo >&2 "Usage: phier [-s <search>] <pid>"
66 echo >&2 "Usage: phier [-s <search>] [-u <user>] [-x] <process>"
67 echo >&2 "Options: -s <search> Stop hierarchy at this process name."
68 echo >&2 " -u <user> Start search from processes owned by user."
69 echo >&2 " -x Match process name exactly."
70 echo >&2 "Example: phier -s sshd $$"
71 echo >&2 "Example: phier -s sshd -u iain nxagent"
76 if ! echo "$children" | grep -qs '^[0-9]*$'; then
77 children=$(pgrep $pgrep_opts "$children")
81 for child in $children; do
82 parent=$(find_parent "$child" "$top")