-
Notifications
You must be signed in to change notification settings - Fork 2
/
killapp
executable file
·157 lines (138 loc) · 4.64 KB
/
killapp
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
#!/bin/bash
set -e
function __print_help_and_exit__()
{
command echo "DESCRIPTION"
command echo "Kill user processes matching to a regular expression. A confirmation is prompted before sending KILL signal."
command echo
command echo "USAGE"
command echo "killapp [--noprompt] [--echo-only] <regexp>"
command echo
command echo " --force Skip the confirmation prompt."
command echo " --echo-only Kill signal commands are printed but not executed."
command echo " --all-users Don't filter processes by $USER. May need sudo to actually send KILL."
command echo " --sudo Send KILL signal with sudo."
command echo
command echo "grep is run in case-sensitive mode only if <pattern> contains at least one upper letter (similar to smartcase in vim)."
exit 1
}
# global variables
reg_exp=""
mode_force=0
mode_echo_only=0
mode_all_users=0
mode_sudo=0
kill_pids=""
function __send_kill__()
{
if [ $mode_sudo -eq 0 ]; then
command kill -s KILL $kill_pids
command echo KILLED
else
command sudo kill -s KILL $kill_pids
command echo KILLED
fi
}
function __kill__()
{
local grep_args=""
# check reg_exp contains upper letter
if [[ "$reg_exp" =~ [[:upper:]] ]]
then
#echo "reg_exp contains upper letter(s), case-sensitive mode."
grep_args=""
else
#echo "reg_exp does not contain an upper letter, case-insensitive mode."
grep_args="-i"
fi
if [ $mode_all_users -eq 0 ]; then
out=`command ps aux | command grep "^$USER" | command grep $grep_args "$reg_exp"`
echo command grep $grep_args "$reg_exp"
command ps aux | command grep "^$USER" | command grep $grep_args "$reg_exp"
else
out=`command ps aux | command grep $grep_args "$reg_exp"`
echo command grep $grep_args "$reg_exp"
command ps aux | command grep $grep_args "$reg_exp"
fi
grep_no_quote="grep $grep_args $reg_exp"
# https://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
lines_exclude='== EXCLUDED =='
lines_include='== INCLUDED =='
any_exclude=0
any_include=0
# loop over lines
while read -r line; do
arr=($line)
pid="${arr[1]}"
exclude=0
if [[ $line == *${SCRIPTPATH}* ]]; then exclude=1; fi # if line contains the current script
if [[ $line == *${grep_no_quote}* ]]; then exclude=1; fi # if line contains the grep command
if [[ $$ == $pid ]]; then exclude=1; fi # if equal to pid of the script
if [[ $$ == $grep_pid ]]; then exclude=1; fi # if equal to pid of the grep subprocess
if [[ $exclude == 1 ]]; then
lines_exclude="$lines_exclude\n$line"
any_exclude=1
else
lines_include="$lines_include\n$line"
kill_pids="$kill_pids $pid"
any_include=1
fi
done <<< "$out"
if [[ $any_exclude == 1 ]]; then
command echo -e "$lines_exclude"
command echo
fi
if [[ $any_include == 1 ]]; then
command echo -e "$lines_include"
command echo
command echo command kill -s KILL $kill_pids
if [ $mode_echo_only -eq 0 ]; then
if [ $mode_force -eq 0 ]; then
read -p "Are you sure? [y/n] " -n 1 -r # https://stackoverflow.com/questions/1885525/how-do-i-prompt-a-user-for-confirmation-in-bash-script
command echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
__send_kill__
else
command echo Cancelled.
fi
else
__send_kill__
fi
else
command echo "(No kill signal was sent due to --echo-only)"
fi
else
command echo "No matching process."
fi
}
function __main__()
{
local narg=$#
if [ $narg -eq 0 ]
then
__print_help_and_exit__
fi
# go over the options
local execargs=()
for (( argi=1; argi<$narg; argi++ )); do
if [ "${!argi}" == "--force" ]; then
mode_force=1
elif [ "${!argi}" == "--echo-only" ]; then
mode_echo_only=1
elif [ "${!argi}" == "--all-users" ]; then
mode_all_users=1
elif [ "${!argi}" == "--sudo" ]; then
mode_sudo=1
else
command echo "Unrecognized option: ${!argi}"
command echo ""
__print_help_and_exit__
fi
done
reg_exp="${!narg}"
__kill__
}
# ok with multiple quoted arguments
__main__ "$@"