← All MicroBlogs
Kill Process Running on a Specific Port
February 05, 2026
devopsterminalnetworkingtroubleshooting
Kill Process Running on a Specific Port
When you encounter "Port X is already in use" errors, you need to find and kill the process occupying that port.
Windows (PowerShell)
Find the process using the port
netstat -ano | findstr :4221Output example:
TCP [::1]:4221 [::]:0 LISTENING 3912The last number (3912) is the Process ID (PID).
Kill the process
taskkill /F /PID 3912One-liner (find and kill)
# Replace 4221 with your port number
$port = 4221
$pid = (Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue).OwningProcess
if ($pid) { Stop-Process -Id $pid -Force; Write-Host "Killed process $pid" } else { Write-Host "No process on port $port" }Linux / macOS (Bash)
Find the process using the port
lsof -i :4221Or using netstat:
netstat -tulpn | grep :4221Kill the process
kill -9 3912One-liner (find and kill)
# Replace 4221 with your port number
kill -9 $(lsof -t -i:4221) 2>/dev/null && echo "Process killed" || echo "No process on port 4221"Or using fuser:
fuser -k 4221/tcpCommon Use Cases
- Dev server conflicts: When Angular/React/Node dev servers don't shut down properly
- Docker port binding: When containers leave ports occupied after stopping
- Database connections: When database servers (PostgreSQL, MySQL) hold onto ports
Pro Tip
Add these as shell aliases for quick access:
PowerShell (add to $PROFILE):
function killport($port) {
$p = (Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue).OwningProcess
if ($p) { Stop-Process -Id $p -Force; "Killed PID $p on port $port" } else { "No process on port $port" }
}Bash (add to ~/.bashrc or ~/.zshrc):
killport() { kill -9 $(lsof -t -i:$1) 2>/dev/null && echo "Killed process on port $1" || echo "No process on port $1"; }Then simply run: killport 4221
This will save you time and frustration when dealing with port conflicts!