-
Notifications
You must be signed in to change notification settings - Fork 3
/
die
executable file
·61 lines (53 loc) · 1.21 KB
/
die
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
#!/bin/bash
# Kills processes based on process name.
usage() {
echo "usage: ${0##*/} [options] REGEX" 1>&2
echo "" 1>&2
echo "kills all processes matching REGEX." 1>&2
echo ""
echo "options:"
echo " -n enables dryrun mode" 1>&2
echo " -q enables quite mode" 1>&2
echo " -h or --help shows usage help" 1>&2
exit 1
}
if echo "$*" | grep -Eq -- '--help\b|-h\b'; then
usage
fi
DRYRUN=0
QUIET=0
while getopts "hqn" opt; do
case "$opt" in
h) usage ;;
n) DRYRUN=1 ;;
q) QUIET=1 ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
# shellcheck disable=SC2009
LIST="$(ps -ww -ew -o pid,user,command | grep -Ev "bin/die|grep" | grep -Ei "$1")"
if [ "$LIST" = "" ]; then
if [[ $QUIET == 0 ]]; then
echo "no processes found"
fi
exit 0
fi
if [[ $QUIET == 0 ]]; then
echo "$(echo "$LIST" | wc -l) processes found:"
echo "$LIST"
fi
KCMD="kill -9 $(echo "$LIST" | sed "s/^ *//g" | cut -d" " -f1 | tr '\n' ' ')"
if [[ $DRYRUN == 0 ]]; then
if [[ $QUIET == 0 ]]; then
echo -n "running '$KCMD' ... "
fi
eval "$KCMD"
else
if [[ $QUIET == 0 ]]; then
echo -n "dryrun '$KCMD' ... "
fi
fi
if [[ $QUIET == 0 ]]; then
echo "done"
fi