-
Notifications
You must be signed in to change notification settings - Fork 2
/
prok.sh
168 lines (141 loc) · 4.19 KB
/
prok.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
#!/usr/bin/env bash
prok_main() {
local pids
local ps_mods="u" # ps -u
local pgrep_params
pgrep_params="$(gen_pgrep_params)"
if [ -z "$PATTERN" ]; then
# word splitting in $pgrep_params is intended
# shellcheck disable=SC2086
pids="$(pgrep $pgrep_params)"
else
# word splitting in $pgrep_params is intended
# shellcheck disable=SC2086
pids="$(pgrep $pgrep_params "$PATTERN")"
fi
pids="$(echo "$pids" | grep -v "$$")"
if [ -z "$pids" ]; then
echo "No processes found"
exit
fi
pids="$(echo "$pids"| tr '\n' ' ')"
if [ -n "$FOREST" ]; then
local temp_pids=""
for pid in $pids
do
temp_pids+="$(proctree "$pid")"
done
pids="$temp_pids"
fi
if [[ -n "$FOREST" && "$(uname)" == "Linux" ]]; then
ps_mods+="f"
fi
# stackoverflow copypaste. It's a pity you're seeing this.
pids="${pids%"${pids##*[![:space:]]}"}"
if [ -n "$ps_mods" ]; then
ps "$ps_mods" -p "$pids"
else
ps -p "$pids"
fi
if [ -n "$KILL_THEM_ALL" ]; then
if [ ! -z "$SIGNAL" ]; then
echo -n "Kill each of these processes (with $SIGNAL)"
else
echo -n "Kill each of these processes"
fi
if [ -n "$FOREST" ]; then
echo -n "(whole tree)"
fi
echo "? (y/n)"
read -r KILL_THEM_ALL
if [[ "$KILL_THEM_ALL" = "y" || "$KILL_THEM_ALL" = "yes" ]]; then
# word splitting is intended
# shellcheck disable=SC2086
if [ ! -z "$SIGNAL" ]; then
kill "$SIGNAL" $pids
else
kill $pids
fi
echo "Executed"
else
echo "Abort"
fi
fi
}
gen_pgrep_params() {
if [ -n "$ONLY_MY" ]; then
MATCH_UID="$(id -u)"
elif [ -n "$MATCH_USERNAME" ]; then
MATCH_UID="$(id -u "$MATCH_USERNAME")"
fi
if [ -n "$MATCH_UID" ]; then
echo -n " -u $MATCH_UID"
fi
if [ -z "$ONLY_PROCNAME" ]; then
echo -n " -f"
fi
}
# looks for all pid parents. Couldn't find a better way
proctree () {
proc="$1"
while [ ! "$proc" -eq 1 ]; do
echo -n "${proc} "
proc="$(ps -p "$proc" -o ppid= | tr -d '[:space:]')"
done
}
usage() {
echo "Prok: easy process grep with ps output"
echo
echo "Usage: prok [--user <USERNAME>] [--uid <UID>] [-fmp] [-(1|2|3|9)] [--SIG<SIGNAL>] [<PATTERN>]"
echo
echo "Parameters:"
echo " -f --forest print parents of all matched PID's."
echo " On linux prints with 'ps f'"
echo " -m --my match only processes of current user"
echo " -p --procname match only executable, not full command"
echo " --user USERNAME match only processes of USERNAME"
echo " --uid UID match only processes of UID(numeric)"
echo
echo " --kill ask to kill all matched processes"
echo " --SIG<SIGNAL> do killing with this signal. e.g. --SIGKILL"
echo " -1 -2 -3 -9 do killing with signal's numeric alias"
echo " Be cautious with -f option, it'll bring whole forest down"
echo
}
FOREST=""
ONLY_MY=""
ONLY_PROCNAME=""
MATCH_UID=""
MATCH_USERNAME=""
PATTERN=""
KILL_THEM_ALL=""
SIGNAL=""
combined_opts() {
while getopts "hfmp" opt; do
case $opt in
h) usage; exit;;
f) FOREST="1";;
m) ONLY_MY="1";;
p) ONLY_PROCNAME="1";;
*) ;;
esac
done
}
# for single and long opts
while [ $# -gt 0 ]; do
case "$1" in
(-h|--help) usage; exit; ;;
(-f|--forest) FOREST="1"; shift; ;;
(-m|--my) ONLY_MY="1"; shift; ;;
(-p|--procname) ONLY_PROCNAME="1"; shift; ;;
(-1|-2|-3|-9) SIGNAL="$1"; shift ;;
(--SIG*) SIGNAL="-${1:5}"; shift ;;
(--uid) MATCH_UID="$2"; shift 2; ;;
(--user) MATCH_USERNAME="$2"; shift 2; ;;
(--kill) KILL_THEM_ALL="1"; shift; ;;
(--) shift; PATTERN="$1"; break; ;;
(-*) combined_opts "$1"; shift; ;;
(*) PATTERN="$1"; break; ;;
esac
done
prok_main