Notes fanzru's shorts
Date 12 / 22 / 2024
E = mc²
∇²Ψ + V(x)Ψ = EΨ
∫f(x)dx
Back to Shorts
terminalportsprocessestroubleshooting

Kill Process by Port

2,103 views

Find and kill process on port

bash
# Find process on port 3000
lsof -ti:3000

# Kill process on port 3000
kill -9 $(lsof -ti:3000)

# One-liner to kill port
lsof -ti:3000 | xargs kill -9

Common ports

bash
# Kill common development ports
kill -9 $(lsof -ti:3000)  # React/Next.js
kill -9 $(lsof -ti:8000)  # Python/Django
kill -9 $(lsof -ti:8080)  # Java/Spring
kill -9 $(lsof -ti:5000)  # Flask/Express

Create alias

Add to ~/.zshrc:

bash
# Kill process on port
alias killport="lsof -ti:\$1 | xargs kill -9"

# Usage: killport 3000

Alternative method

bash
# Using netstat (macOS)
sudo lsof -i :3000
sudo kill -9 <PID>

# Using ss (Linux)
ss -tulpn | grep :3000
kill -9 <PID>

No more "port already in use" errors! 🎯