Platter Join the Waitlist

How to kill a process on a port on Mac

Two steps: ask macOS which process is listening on the port, then stop that process. Replace 3000 with your port.

1. Find what’s listening

lsof -nP -iTCP:3000 -sTCP:LISTEN

You get one line per listening socket. On a Mac with a Python server on port 4321, it printed:

COMMAND   PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Python  54452 shylands    5u  IPv6 0x3f1588099a5d4d26      0t0  TCP *:4321 (LISTEN)

The number in the PID column is what you need. -nP keeps addresses and ports as numbers (without it, port 4321 shows up as rwhois), and -sTCP:LISTEN limits the list to servers. More on the flags in lsof for ports on macOS.

2. Stop it

kill 54452

Plain kill sends SIGTERM, which asks the process to shut down cleanly. Dev servers almost always obey. Run the lsof command again: no output means the port is free.

Only if it ignores you:

kill -9 54452

-9 (SIGKILL) can’t be refused, but the process gets no chance to save anything or close its own children, so try plain kill first.

The one-liner, and its trap

The most-copied answer is:

kill -9 $(lsof -ti tcp:3000)

lsof -ti tcp:3000 lists every process with a TCP connection on that port, not just the server. A browser tab or script connected to the server is in that list too. We tested it: with a server on a port and one client connected to it, lsof -ti tcp:PORT returned both process IDs, while the -sTCP:LISTEN version returned only the server.

There’s a second trap. Two different servers can share a port number on IPv4 and IPv6: we found a Node app on [::1]:4321 and a Python server on *:4321 at the same time, and the one-liner would have stopped both. The safer version only targets listeners and doesn’t start with -9:

kill $(lsof -nP -iTCP:3000 -sTCP:LISTEN -t)

Even then, run the lookup on its own first and read the COMMAND column. A port number doesn’t tell you which project the process belongs to.

Ports macOS uses itself

If lsof shows ControlCe on port 5000 or 7000, that’s Control Center’s AirPlay Receiver, not a stray dev server. Don’t kill it; it comes straight back. Turn it off in System Settings → General → AirDrop & Handoff → AirPlay Receiver, or run your app on another port.