Skip to content

Commit 447d010

Browse files
committed
a nice kill_port fn using fzf
1 parent b31cb43 commit 447d010

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

fish/functions.fish

+25
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@ function killf
2121
end
2222
end
2323

24+
# Select a port to kill, by pid, port, or command line
25+
function kill_port
26+
27+
# Function to get the command line for a given PID
28+
function get_command -a pid
29+
ps -p $pid | awk 'NR>1 {for (i=4; i<=NF; i++) {printf "%s ", $i}; print ""}'
30+
end
31+
32+
# Find listening processes, get commands, and format output
33+
lsof -iTCP -sTCP:LISTEN -P | awk '{print $2, $9}' | uniq | tail -n +2 | \
34+
while read -l pid port
35+
set -l command (get_command $pid)
36+
set -l port (string pad -w 8 (string replace 'localhost' '' $port))
37+
set -l pid (string pad --right -w 6 $pid)
38+
echo -e "$pid $port $command" | column
39+
end | \
40+
41+
# Pipe the output to fzf for selection. Grab pid and show pstree
42+
fzf --exact --tac --preview 'pstree -p (echo {} | awk "{print $2}")' --preview-window=down,30% --header "Select a process to kill (PID Command Port):" | \
43+
44+
# Kill the selected process
45+
awk '{print $1}' | xargs -r kill -9
46+
end
47+
48+
2449
function clone --description "clone something, cd into it. install it."
2550
git clone --depth=1 $argv[1]
2651
cd (basename $argv[1] | sed 's/.git$//')

0 commit comments

Comments
 (0)