Skip to content

Instantly share code, notes, and snippets.

@aliinreallife
Created April 27, 2025 05:57
Show Gist options
  • Select an option

  • Save aliinreallife/a8060ecb760d97efea501c9f27da86d8 to your computer and use it in GitHub Desktop.

Select an option

Save aliinreallife/a8060ecb760d97efea501c9f27da86d8 to your computer and use it in GitHub Desktop.
A small shell function to safely find and kill the process using a specific port, with confirmation prompt; works for Fish, Bash, and Zsh.
killport() {
port=$1
pid=$(lsof -t -i :$port)
if [ -n "$pid" ]; then
echo "Process using port $port:"
lsof -i :$port
echo
read -p "Are you sure you want to kill PID $pid? (y/n) " confirm
if [ "$confirm" = "y" ]; then
kill -9 $pid
echo "Killed PID $pid on port $port"
else
echo "Cancelled. No process killed."
fi
else
echo "No process found on port $port"
fi
}
function killport
set port $argv[1]
set pid (lsof -t -i :$port)
if test -n "$pid"
echo "Process using port $port:"
lsof -i :$port
echo
read -P "Are you sure you want to kill PID $pid? (y/n) " confirm
if test "$confirm" = "y"
kill -9 $pid
echo "Killed PID $pid on port $port"
else
echo "Cancelled. No process killed."
end
else
echo "No process found on port $port"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment