Skip to content

Commit eecb015

Browse files
committed
Add exclude to pids.matching
1 parent 635f733 commit eecb015

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

lib/pids.sh

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
function pid.alive() {
1111
local pid="$1"
1212
[[ -z ${pid} ]] && {
13-
error "usage: pid.alive PID"
13+
panel-error "usage: pid.alive PID"
1414
return 1
1515
}
1616

1717
is.numeric "${pid}" || {
18-
error "The argument to pid.alive() must be a numeric Process ID"
19-
return 1
18+
panel-error "The argument to pid.alive() must be a numeric Process ID"
19+
return 1:$
2020
}
2121

2222
[[ -n "${pid}" && -n $(ps -p "${pid}" | grep -v TTY) ]]
@@ -38,12 +38,12 @@ USAGE:
3838
}
3939

4040
is.numeric "${pid}" || {
41-
error "First argument to pid.sig must be numeric."
41+
panel-error "First argument to pid.sig must be numeric."
4242
return 1
4343
}
4444

4545
is.numeric "${signal}" || sig.is-valid "${signal}" || {
46-
error "First argument to pid.sig must be numeric."
46+
panel-error "First argument to pid.sig must be numeric."
4747
return 1
4848
}
4949

@@ -131,52 +131,55 @@ EXAMPLES:
131131

132132
# Normalize search pattern, by inserting a '[' in the beginning
133133
# This only works with regular strings, not a regexp
134-
pids.normalize.search-string() {
135-
local pattern="$*"
134+
function pids.normalize.search-string() {
135+
local pattern="$1"
136136
# convert a simple pattern, eg. "puma" into eg. "[p]uma"
137137
[[ "${pattern:0:1}" == '[' ]] || pattern="[${pattern:0:1}]${pattern:1}"
138138
printf "${pattern}"
139139
}
140140

141-
pids.matching() {
141+
function pids.matching() {
142142
local pattern="${1}"
143+
local exclude_pattern="${2}"
143144

144145
if [[ -z "${pattern}" ]]; then
145146
printf "
146147
DESCRIPTION:
147148
Finds process IDs matching a given string.
148149
149150
USAGE:
150-
${bldgrn}pids.matching string${clr}
151+
${bldgrn}pids.matching match_regex [exclude_regex]${clr}
151152
152153
EXAMPLES:
153-
${bldgrn}pids.matching sidekiq${clr}
154+
# Return all sidekiq pids except the master process
155+
${bldgrn}pids.matching sidekiq master${clr}
154156
"
155157
return 0
156158
fi
157159

158-
pattern="$(pids.normalize.search-string "${pattern}")"
159-
pids.matching.regexp "${pattern}"
160+
local include_pattern="$(pids.normalize.search-string "${pattern}"})"
161+
pids.matching.regexp "${include_pattern}" "${exclude_pattern}"
160162
}
161163

162-
pids.matching.regexp() {
163-
local pattern="${1}"
164+
function pids.matching.regexp() {
165+
local include_pattern="${1}"
166+
local exclude_pattern="${2}"
164167

165-
if [[ -z "${pattern}" ]]; then
168+
if [[ -z "${include_pattern}" ]]; then
166169
printf "
167170
DESCRIPTION:
168171
Finds process IDs matching a given regexp.
169172
170173
USAGE:
171-
${bldgrn}pids.matching regular-expression${clr}
174+
${bldgrn}pids.matching.regexp exclude-regex${clr}
172175
173176
EXAMPLES:
174-
${bldgrn}pids.matching '[s]idekiq\s+' ${clr}
177+
${bldgrn}pids.matching.regexp '[s]idekiq\s+' [ exlcude-pattern ]${clr}
175178
"
176179
return 0
177180
fi
178181

179-
ps -ef | ${GrepCommand} "${pattern}" | ${GrepCommand} -v grep | awk '{print $2}' | sort -n
182+
eval "ps -ef | ${GrepCommand} \"${include_pattern}\" | ${GrepCommand} -v \"grep${exclude_pattern}\" | awk '{print \$2}' | sort -n"
180183
}
181184

182185
# prints PIDs with other information such as CPU, MEM, etc.

0 commit comments

Comments
 (0)