#!/bin/bash # # $Id$ # # phier: Show process tree incorporating a process. # Notes: phier functions similarly (but not identically) to Solaris ptree. # Usage: phier [options] # Options: -s Stop hierarchy at this process name. # Example: phier -s sshd $$ # function find_parent() { child="$1"; shift top="$1"; shift if [ -z "$child" ]; then echo >&2 "No child to find." return 1 fi cmd=$(ps -o comm= -p $child) pid=$(ps -o pid= -p $child) ppid=$(ps -o ppid= -p $child) # echo >&2 "Finding from $child ($top) $pid/$ppid/$cmd" if [ -z "$pid" ]; then echo >&2 "Can't find PID $child!" return 1 fi if [ ! -z "$top" ]; then if [ "$cmd" = "$top" ]; then echo $child return 0 fi fi if [ $pid = 1 -o $ppid = 1 ]; then echo $pid return 0 fi find_parent $ppid $top return $? } # Parse arguments. top= while getopts ":s:" opt; do case $opt in s) top="$1";; esac done shift $((OPTIND-1)) if [ $# -lt 1 ]; then echo >&2 "phier: Print process hierarchy for a process." echo >&2 "Usage: phier [options] " echo >&2 "Options: -s Stop hierarchy at this process name." echo >&2 "Example: phier -s sshd $$" exit 1 fi parent=$(find_parent "$1" "$top") if [ $? = 0 ]; then pstree -ulap $parent else exit 1 fi