Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Low-power
Created November 28, 2018 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Low-power/dca7320d27a523b304ef9605b9d9870b to your computer and use it in GitHub Desktop.
Save Low-power/dca7320d27a523b304ef9605b9d9870b to your computer and use it in GitHub Desktop.
#!/bin/bash
#!/bin/sh
# pstree
# Copyright 2015-2018 Rivoreo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Compatible with ash and bash; incompatible with ksh; untested with other shells.
ORIG_IFS="$IFS"
# Get the process list at once, otherwise this script will running very slowly
process_list="`ps -A -o pid= -o ppid=`"
find_children() {
#printf "function: find_children $* (#=$#)\\n"
if [ $# -lt 1 ]; then
echo "Usage: find_children <pid> [<treeline>] [...]" 1>&2
return 255
fi
#local level="$2"
#[ -z "$level" ] && level=0
#local i="$level"
#while [ $i -gt 0 ]; do printf " "; i=$((i-1)); done
local current_pid="$1"
local current_ps_line
current_ps_line="`ps -o pid= -o comm= -p \"$current_pid\"`" || return 1
shift
local i=0
for c in "$@"; do
i=$((i+1))
[ "$c" = " " -a $i = $# ] && c=\\
printf " %c" "$c"
done
printf -- "-- "
echo "$current_ps_line" | sed -r 's/^ +//'
local children=
i=0
#printf %s\\n "$process_list" | while read pids; do
IFS="
"
for pids in $process_list; do
#pid="`echo \"$pids\" | sed -r -e 's/ +[0-9]+$//' -e 's/^ *//'`"
#ppid="`echo \"$pids\" | sed -r 's/^ *[0-9]+ +//'`"
pid=
ppid=
IFS="$ORIG_IFS"
for n in $pids; do
[ -z "$pid" ] && pid=$n || ppid=$n
done
#echo "\"$pid\" \"$ppid\""
[ "$ppid" = $$ -o "$pid" = $$ -o "$pid" = 0 ] && continue
#[ "$ppid" = "$1" ] && find_children "$pid" $((level+1))
#[ "$ppid" = "$current_pid" ] && find_children "$pid" "$@" "|"
[ "$ppid" = "$current_pid" ] && children="$children
$pid" && i=$((i+1))
done
IFS="
"
#echo "$children"
for pid in $children; do
i=$((i-1))
if [ $i = 0 ]; then
find_children $pid "$@" " "
else
find_children $pid "$@" "|"
fi
done
}
rootpid="$1"
[ -z "$rootpid" ] && rootpid=1
find_children "$rootpid"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment