-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathptree
executable file
·78 lines (69 loc) · 1.71 KB
/
ptree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# $Id: ptree,v 1.11 2015/09/23 17:09:26 root Exp $
# ptree only supports one and exactly one PID so forget other args..
#MPSARGS="-planA" ### ASCII
MPSARGS="-planG" ### Graphics
# Initialization
myPID=$1
myPPID=${myPID}
myTOP=${myPID}
#Sub-routines
errecho() {
>&2 echo "$*"
}
CheckPID() {
# ptree only supports one and exactly one PID so forget about other args..
if [ "x$1" = "x" ]; then
errecho "no PID specified!"
return 0
fi
# Verify PID's presence
ps --no-heading -o pid ${myPID} > /dev/null 2>&1 || {
errecho "PID $myPID does not exist!"
return 1
}
return 0
}
GetPPID() {
CheckPID $1 || exit 127
localPPID=$(awk '{ if ( $1 ~ "PPid:" ) print $2 }' /proc/$1/status)
[ -f /proc/${localPPID}/status ] && {
localPName=$(awk '{ if ( $1 ~ "Name:" ) print $2 }' /proc/${localPPID}/status)
# localPPID=`ps --no-heading -o ppid ${myPPID} 2> /dev/null|xargs`
[ "x${localPPID}" != "x" ] && {
[ "x${localPName}" != "x" -a "x${localPName}" != "xinit" -a "x${localPName}" != "xsystemd" -a "x${localPName}" != "xscreen" ] && {
echo ${localPPID}; return 0
} || {
# errecho "Discarded PPID: ${localPPID}"
echo 0 ; return 1
}
} || {
echo 0 ; return 1
}
} || {
echo 0 ; return 1
}
return 0
}
#
# Sanity Check
if [ "x$(uname -s)" != "xLinux" ]; then
echo "Not supported on $(uname -s)! Exit!"
exit 127
fi
# Sanity Check (2)
CheckPID ${myPID} || exit 127
# Main routine
if [ "x${myTOP}" != "x" ]; then
# Walk up to Find Parent until myPPID=1
while [ ${myTOP} -gt 0 ]; do
myTOP=$(GetPPID ${myPPID})
if [ ${myTOP} -gt 1 ]; then
myPPID=${myTOP}
## echo "Found PPID: ${myPPID}"
fi
done
exec /usr/bin/pstree ${MPSARGS} ${myPPID}
else
exec /usr/bin/pstree ${MPSARGS}
fi