forked from Zeioth/fzf-kill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfzf-kill-legacy.sh
More file actions
216 lines (177 loc) · 6.46 KB
/
fzf-kill-legacy.sh
File metadata and controls
216 lines (177 loc) · 6.46 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
# fzf-kill macOS Port
# Based on: https://github.com/Zeioth/fzf-kill.git
# PARAMETERS THAT CAN BE OVERRIDED BY THE USER (DEFAULTS)
DETAILED_MODE="false"
LOOP_MODE="false"
EXCLUDE_LIST='myprogramtoexcludehere'
# Export to ensure fzf subshell sees the prompt
export FZF_DEFAULT_OPTS="--min-height=100 --prompt 'kill -9 '"
SHOW_ROOT="false"
SHOW_HELP="false"
# macOS portable User ID detection (macOS doesn't always set $UID)
CURRENT_UID=$(id -u)
# PARSE PARAMETERS
# ========================================================
# Loop through all command line arguments
for arg in "$@"; do
# Check if the argument contains the "all" string
if echo "$arg" | grep -q "all"; then
DETAILED_MODE="true"
fi
# Check if the argument contains the "loop" string
if echo "$arg" | grep -q "loop"; then
LOOP_MODE="true"
fi
# Check if the argument contains the "exclude=" string
if echo "$arg" | grep -q "exclude="; then
# Use cut to extract everything after the "=" symbol
EXCLUDE_LIST=$(echo "$arg" | cut -d= -f2-)
fi
# Check if the argument contains the "fzf_default_ops=" string
if echo "$arg" | grep -q "fzf_default_ops="; then
# Use cut to extract everything after the "=" symbol
export FZF_DEFAULT_OPTS=$(echo "$arg" | cut -d= -f2-)
fi
# Check if the argument contains the "showroot" string
if echo "$arg" | grep -q "showroot"; then
# Pretend we are root during this program
SHOW_ROOT="true"
fi
# Check if the argument contains the "help" string
if echo "$arg" | grep -q "help"; then
# Use cut to extract everything after the "=" symbol
SHOW_HELP="true"
fi
done
# FUNCTIONS
# ========================================================
# CONSTANTS
# macOS ps -ef column 2 is PID, column 8 is CMD
EXTRACT_COLS_2_AND_8='{print $2, $8}'
detailed_mode(){
# Advanced mode (show subprocesses, if one is selected, parent is killed)
if [[ "$CURRENT_UID" != "0" && "$SHOW_ROOT" == "false" ]]; then
pid=$(ps -u "$CURRENT_UID" -ef | \
# Eliminate headers and started by this command.
# macOS: BSD head doesn't support negative lines. We use sed to trim the last 6.
sed 1d | sed -n -e :a -e '1,6!P;N;D;ba' | \
# Exclude specific words from the output
grep -vE "($EXCLUDE_LIST|fzf-kill)" | \
# pipe to fzf
fzf -m | \
# Store the user selection on the pid variable
# macOS ps -ef PID is column 2
awk '{print $2}')
else
pid=$(ps -ef | \
# Eliminate headers
sed 1d | \
# Exclude specific words from the output
grep -vE "($EXCLUDE_LIST|fzf-kill)" | \
# pipe to fzf
fzf -m | \
# Store the user selection on the pid variable
awk '{print $2}')
fi
if [[ "x$pid" != "x" ]]; then
# Kill the process, no mater if child or parent is selected.
# macOS: Manual loop ensures stability as BSD ps -o ppid requires a valid PID.
for p in $pid; do
ppid=$(ps -p "$p" -o ppid= | tr -d ' ')
[[ -n "$ppid" ]] && kill "-${1:-9}" "$ppid" > /dev/null 2>&1
kill -9 "$p" > /dev/null 2>&1
done
fi
}
simple_mode(){
# Simple mode (We only list parent processes)
# (Slower but more visually appealing).
if [[ "$CURRENT_UID" != "0" && "$SHOW_ROOT" == "false" ]]; then
# Extract running processes
pid=$(ps -u "$CURRENT_UID" -ef | \
# Extract cols 2 and 8
awk "$EXTRACT_COLS_2_AND_8" | \
# Eliminate headers and started by this command.
sed 1d | \
# Exclude specific words from the output
grep -vE "($EXCLUDE_LIST|fzf-kill)" | \
# De-duplicate entries
awk '{if($2 in seen){}else{seen[$2]=$0; print}}' | \
# Get parent process
# macOS: BSD ps fails on comma-lists if a PID is missing. xargs -n 1 handles them safely.
# macOS: BSD requires separate -o flags for each column.
awk '{print $1}' | xargs -n 1 -I {} ps -p {} -o pid= -o comm= 2>/dev/null | \
# pipe to fzf
fzf -m | \
# Store the user selection on the pid variable
awk '{print $1}')
else
pid=$(ps -ef | \
# Extract cols 2 and 8
awk "$EXTRACT_COLS_2_AND_8" | \
# Eliminate headers and started by this command.
sed 1d | \
# Exclude specific words from the output
grep -vE "($EXCLUDE_LIST|fzf-kill)" | \
# De-duplicate entries
awk '{if($2 in seen){}else{seen[$2]=$0; print}}' | \
# Get parent process
awk '{print $1}' | xargs -n 1 -I {} ps -p {} -o pid= -o comm= 2>/dev/null | \
# pipe to fzf
fzf -m | \
# Store the user selection on the pid variable
awk '{print $1}')
fi
if [[ "x$pid" != "x" ]]; then
# Kill the parent process
kill -9 $pid 2>&1
fi
}
show_help(){
echo "HELP:
fzf-kill allow you to kill your programs in a quick and intuitive way
without the cluttered user experience,
or the uncomfortable keybindings of other task killers.
# BASIC COMMANDS
* --parents Show only parent processes. (default)
* --all Show both parent and children processes.
# ADVANCED OPTIONS
* --exclude You can exclude a list of programs from appearing on the list.
Use it like:
fzf-kill --exclude='word1|word2|word2'
You can also pipe it from a file like:
fzf-kill --exclude=\"\$(cat ~/.config/fzf-kill/my_excludes.txt)\"
* --loop fzf-kill will stay open even after killing a program.
* --fzf_default_ops You can use it to override the env var FZF_DEFAULT_OPTS.
Use it like:
fzf-kill --fzf_default_ops=\"--min-height=100 --prompt 'kill -9 '\"
* --showroot List both root and user processes running in the session.
By default is disabled. Meaning by default we show only
processes owned by the user.
Please note that this option won't let you kill anything
launched by root unless you are root, or run fzf-kill with
sudo privileges (which by general rule, you shouldn't).
"
}
# ENTRY POINT
# ========================================================
# Do while
do_program="true"
while [ "$do_program" = "true" ] ; do
# Help
if [ "$SHOW_HELP" = "true" ]; then
show_help
break
fi
# Code
if [ "$DETAILED_MODE" = "true" ]; then
detailed_mode
else
simple_mode
fi
# Exit condition
if [ "$LOOP_MODE" = "false" ]; then
do_program="false"
fi
done