A two-shell killport
A tiny killport <port> function for Git Bash and PowerShell, and the one filter that makes it correct.
Windows dev-loop frustration: vite throws EADDRINUSE, the fix is always netstat -ano | grep :5173, eyeball the PID, taskkill /PID <pid> /F. It’s eight seconds I don’t have, repeated every time a dev server fails to clean up after itself.
killport 5173 does all three.
It’s two snippets, one per shell I actually use on Windows. Git Bash gets a function sourced from ~/.bashrc; PowerShell gets one dot-sourced from $PROFILE. No cross-shell shims – the Bash side uses netstat/taskkill, the PowerShell side uses native Get-NetTCPConnection/Stop-Process cmdlets.
killport() {
if [ -z "$1" ]; then
echo "usage: killport <port>" >&2
return 1
fi
local pid
pid=$(netstat -ano | grep ":$1 " | grep LISTENING | awk '{print $5}' | head -n1)
if [ -z "$pid" ]; then
echo "no LISTENING process on port $1"
return 0
fi
echo "killing PID $pid on port $1"
taskkill //PID "$pid" //F
}The only decision worth calling out is the LISTENING filter. netstat -ano returns every socket touching that port – not just the server. If you’re running curl localhost:5173 in one pane and killport 5173 in another, the ESTABLISHED socket the curl is using also has 5173 in its output. Kill by PID without filtering and you can kill the curl, not the server. TIME_WAIT sockets can hang around for minutes after a connection closes – they’d show up too, pointing at PIDs that have moved on.
Filtering to LISTENING drops all of that. The only socket in a LISTENING state on that port is the server itself.
The PowerShell side is shorter because Get-NetTCPConnection takes -State Listen directly – no pipe, no grep needed. One PID, killed.
Projects
what this writeup is about