August 28, 2024
Simple ZSH Utility to Kill a Port
#Overview
Whenever something bad happens (generally Intellij crashing), I find myself trying to restart applications and often seeing this error:
Error
Failed to start server Error: listen EADDRINUSE: address already in use :::3000 at Server.setupListenHandle [as _listen2] (node:net:1904:16) at listenInCluster (node:net:1961:12) at Server.listen (node:net:2063:7) at Server.listen (node:net:2063:7)
Then, I'd proceed to google How to find and kill port
, which always led me to the many responses on Stack Overflow
#Solution
I added this to my ~/.zshrc
and never looked back.
killport() {
for port in "$@"; do
lsof -ti:$port | xargs kill -9
done
}
alias kp="killport"
zshrc
which means you can:
killport 3000
killport 3000 8080
zsh
or for brevity :)
kp 3000
kp 3000 8080
zsh