-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.sh
executable file
·195 lines (174 loc) · 4.76 KB
/
proc.sh
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
#
# proc.sh: psa, psf, procs
# select and display particular processes
#
# https://github.com/smemsh/utilsh/
# https://spdx.org/licenses/GPL-2.0
#
##############################################################################
if [[ $1 == -p ]]
then
psflags="-o pid="
cmdflags="$psflags"
shift
else
psfields=(
pid ppid pgid sid
pcpu
rss vsz tty
s
cmd
)
IFS=,
psflags="-wwH --no-headers -o ${psfields[*]}"
unset IFS
cmdflags="-o comm="
fi
gawk_psfmt="pid=,pgid=,ppid="
gawk_filter='
{
pid = $1; pgid = $2; ppid = $3
if (exclpg && pgid == exclpg) next
if (ppid == 2 && exclk) next
results[pid]++
}'
gawk_end_print='
END {
if (recursive) recurse(args)
$0 = ""; n = 1; OFS=","
for (pid in results) $(n++) = pid
print
}'
gawk_filter_print="
$gawk_filter
$gawk_end_print
"
# by pattern match in command line, or give full process table without arg
#
psa ()
{
local psargs
local pids
local uname=`uname -s`
# TODO: use *TYPE variables provided by bash
if [[ $uname == 'Linux' ]]
then
(($# == 0)) && set -- ^
(($# > 1)) && bomb "only one pattern for pgrep"
matches=$(pgrep -d, -f "$1")
[[ $matches ]] || exit
pids=$(ps -p $matches -o $gawk_psfmt \
| gawk -v exclpg=$$ -v exclk=1 "$gawk_filter_print")
[[ $pids ]] && ps $psflags -p $pids || false
return # end Linux
elif [[ $uname == 'SunOS' ]]; then psargs=ef
elif [[ $uname == 'Darwin' ]]; then psargs=efwwwwww
else echo "no code for you!"
fi
# avoid our own process appearing: capture ps output first so the greps
# aren't therein, and try to remove our own process from the list, by
# escaping the expected ps line for ourself, which could have arbitrary
# patterns given as $1, so we escape all the chars and require it has
# our pid in a field of its own
#
# todo: tests well on linux, but need to try solaris and bsd, which is
# the only place it will ever actually run. tried to avoid gnuisms...
#
psout="$(ps -$psargs)"
sedcmds='s,[^^],[&],g; s,\^,\\^,g'
escprog="$(sed "$sedcmds" <<< "$0")"
escpat="$(sed "$sedcmds" <<< "$1")"
grep -E "$1" <<< "$psout" \
| grep -vE "\\b$$\\b.*[[:space:]]$escprog[[:space:]]+$escpat\$"
}
# processes with a session leader matching specified name
#
psl ()
{
local pids=$(ps -eo pid=,sid=,comm= \
| gawk -v leadername="${1:?}" '
{
pid = $1; sid = $2; cmd = $3
pids[i] = pid; sids[i] = sid
if (pid == sid) leaders[i] = 1
if (sid != 0 && leaders[i] && cmd == leadername)
target_sessions[sid] = 1
i++
}
END {
pidlen = length(pids)
for (i = 0; i < pidlen; i++) {
sid = sids[i]
if (target_sessions[sid] && !leaders[i])
matches[j++] = pids[i]
}
sidlist = matches[0]
matchlen = length(matches)
for (i = 1; i < matchlen; i++)
sidlist = sidlist "," matches[i]
printf("%s", sidlist)
}')
[[ $pids ]] && ps $psflags -p $pids
}
# children of given parent(s), recursively
# -p display pids only, which will not include parent
#
psf ()
{
local pids=$(ps -eo pid=,ppid= \
| gawk -v ourpid=$$ -v recursive=1 -v pidlist="$*" '
BEGIN {
split(pidlist, arglist, "([[:space:]]+|,)")
for (arg in arglist) args[arglist[arg]]++
}
function \
recurse(pids)
{
for (pid in pids) {
if (pid == ourpid) continue
results[pid]++
if (length(children[pid])) recurse(children[pid])
}
}
{
pid = $1; ppid = $2
children[ppid][pid]++
}'"$gawk_end_print")
[[ $pids ]] && ps $psflags -p $pids
}
pspg ()
{
local pidlist=$(IFS=,; pgrep --pgroup "${*:?}")
[[ $pidlist ]] && ps $psflags -p $pidlist
}
# we commonly want to remove processes with our own pgid from the list,
# but otherwise use just one selector option that takes a list
#
ps_noself_select ()
{
local optname=${1:?}; shift
local pids=$(ps --$optname "$*" -o $gawk_psfmt \
| gawk -v exclpg=${exclpg:-$$} -v exclk=$exclk "$gawk_filter_print")
[[ $pids ]] && ps $psflags -p $pids
}
pst () { ps_noself_select tty "$@"; }
pss () { ps_noself_select sid "$@"; }
pspp () { ps_noself_select ppid "$@"; }
psp () { exclpg=0 \
ps_noself_select pid "$@"; }
# tabular lists of unique process names matching eponymous criteria
mktable () { sort | uniq | column -c 80; }
procs () { ps -N --ppid=2 ${cmdflags} | mktable; } # non-kernel procs
daemons () { ps --ppid=1 ${cmdflags} | mktable; } # descendants of init
kthreads () { ps --ppid=2 ${cmdflags} | mktable; } # kernel threads
sessions () { ps -Nd ${cmdflags} | mktable; } # session leaders
leaders () { leaders "$@"; } # sessions alias
headless () { psflags="$cmdflags" exclk=1 \
ps_noself_select tty - | mktable; } # no ctty, non-kernel
##############################################################################
invname=${0##*/}
if ! [[ `declare -F $invname` ]]
then echo "unimplemented" >&2; false; exit; fi
$invname "$@"