Bash

Kill Process by Port

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
When a port is "already in use" — find the offending process and (carefully) kill it. `lsof` is the gold standard; `ss` is the lighter modern alternative.
Bash
Raw
# Find what owns port 8080
lsof -ti :8080                # just PIDs (good for piping)
lsof -i :8080                 # full info (PID, user, command, etc.)

# Same with ss (no lsof install needed on modern distros)
ss -tlnp 'sport = :8080'

# Kill whoever holds it
kill "$(lsof -ti :8080)"          # SIGTERM
kill -9 "$(lsof -ti :8080)"       # SIGKILL (use only if SIGTERM ignored)

# Helper
killport() {
    local pids
    pids="$(lsof -ti ":$1")" || { echo "Nothing on port $1"; return 1; }
    kill "$pids"
    echo "killed PIDs: $pids"
}
killport 3000
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.