Skip to content

Commit

Permalink
a nice kill_port fn using fzf
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Oct 2, 2024
1 parent b31cb43 commit 447d010
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions fish/functions.fish
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ function killf
end
end

# Select a port to kill, by pid, port, or command line
function kill_port

# Function to get the command line for a given PID
function get_command -a pid
ps -p $pid | awk 'NR>1 {for (i=4; i<=NF; i++) {printf "%s ", $i}; print ""}'
end

# Find listening processes, get commands, and format output
lsof -iTCP -sTCP:LISTEN -P | awk '{print $2, $9}' | uniq | tail -n +2 | \
while read -l pid port
set -l command (get_command $pid)
set -l port (string pad -w 8 (string replace 'localhost' '' $port))
set -l pid (string pad --right -w 6 $pid)
echo -e "$pid $port $command" | column
end | \

# Pipe the output to fzf for selection. Grab pid and show pstree
fzf --exact --tac --preview 'pstree -p (echo {} | awk "{print $2}")' --preview-window=down,30% --header "Select a process to kill (PID Command Port):" | \

# Kill the selected process
awk '{print $1}' | xargs -r kill -9
end


function clone --description "clone something, cd into it. install it."
git clone --depth=1 $argv[1]
cd (basename $argv[1] | sed 's/.git$//')
Expand Down

0 comments on commit 447d010

Please sign in to comment.