From 4b6eca4e3aa662eefc0a49d8400ed853bfe164e4 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Wed, 3 Oct 2007 07:32:41 +0000 Subject: [PATCH] phier now matches processes and PIDs like pgrep. git-svn-id: https://svn.cambridge.iain.cx/profile/trunk@76 6be0d1a5-5cfe-0310-89b6-964be062b18b --- phier | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/phier b/phier index eaa1b14..cc6387f 100755 --- a/phier +++ b/phier @@ -4,9 +4,13 @@ # # phier: Show process tree incorporating a process. # Notes: phier functions similarly (but not identically) to Solaris ptree. -# Usage: phier [options] +# Usage: phier [-s ] +# Usage: phier [-s ] [-u ] [-x] # Options: -s Stop hierarchy at this process name. +# -u Start search from processes owned by user. +# -x Match process name exactly. # Example: phier -s sshd $$ +# Example: phier -s sshd -u iain nxagent # function find_parent() { @@ -46,25 +50,43 @@ function find_parent() { } # Parse arguments. +children= top= -while getopts ":s:" opt; do +pgrep_opts= +while getopts ":s:u:x" opt; do case $opt in - s) top="$1";; + s) top="$OPTARG";; + u) pgrep_opts="$pgrep_opts -u $OPTARG";; + x) pgrep_opts="$pgrep_opts -x" 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 "Usage: phier [-s ] " + echo >&2 "Usage: phier [-s ] [-u ] [-x] " echo >&2 "Options: -s Stop hierarchy at this process name." + echo >&2 " -u Start search from processes owned by user." + echo >&2 " -x Match process name exactly." echo >&2 "Example: phier -s sshd $$" + echo >&2 "Example: phier -s sshd -u iain nxagent" exit 1 fi -parent=$(find_parent "$1" "$top") -if [ $? = 0 ]; then - pstree -ulap $parent -else - exit 1 +children="$1"; shift +if ! echo "$children" | grep -qs '^[0-9]*$'; then + children=$(pgrep $pgrep_opts "$children") fi + +errors=0 +for child in $children; do + parent=$(find_parent "$child" "$top") + if [ $? = 0 ]; then + pstree -ulap $parent + else + errors=$((errors+1)) + fi +done + +exit $errors -- 2.7.4