Created
April 27, 2025 05:57
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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