Back to lessons

Apple Terminal

Stop the Process Blocking a Dev Port

A stale local server keeps listening on a port after its terminal window was closed.

Command

lsof -ti tcp:3000 | xargs kill

What changed

The process IDs returned by lsof receive a normal TERM signal.

Danger

caution

When to use it

Use when you are confident the process on that port is your stale local dev server.

When not to use it

Do not use on ports for databases, sync tools, or services you do not recognize.

Undo or recovery

Restart the stopped application or dev server if you killed the wrong process.

Expected output

Usually no output. Running the port check again should show no listener.

demo script

Disposable terminal steps

  1. printf '%s\n' 4242 4243
  2. printf '%s\n' 4242 4243 | xargs -n1 printf 'would kill pid %s\n'

simulated output

What it looks like

disposable vessel
::fixture-ready::
$ printf '%s\n' 4242 4243
4242
4243
::exit-code::0
$ printf '%s\n' 4242 4243 | xargs -n1 printf 'would kill pid %s\n'
would kill pid 4242
would kill pid 4243
::exit-code::0

YouTube Short

Free a stuck port.

lsof can output just the PID, and xargs can send a normal kill signal. Only do this when you know what owns the port.

LinkedIn hook

Free a stuck dev port without hunting through Activity Monitor.

Question: Do you prefer killing stale dev servers by PID, port, or process name?

experiments

A/B tests to run

Metric: completion_rate

A: Explain the caution before showing the command.

B: Show the command first, then explain the caution.