Blog Find and kill the process hogging a port

Find and kill the process hogging a port

2 min read

Find and kill the process hogging a port
TL;DR When a port is taken, find the process holding it with lsof -i :PORT, then stop it with kill using its PID. On most systems lsof -ti :PORT gives you the PID directly, so you can find and kill in one line.

You start your dev server and get EADDRINUSE or "port already in use." Almost always it is a previous run of your own server that never died. Here is how to find the culprit and stop it, without rebooting or randomly changing ports.

Find what is holding the port

On macOS and Linux, lsof lists open files, and a listening port is one of them:

lsof -i :3000

You get the process name, its PID, and the user. That is usually enough to recognise a stray copy of your own server.

Kill it

Take the PID from the output and stop it:

kill 12345        # ask it to shut down cleanly
kill -9 12345     # force it, if it ignores the first

Try a plain kill first so the process can shut down gracefully. Reach for -9 only if it refuses.

Do it in one line

lsof -t prints just the PID, which you can pipe straight into kill:

kill $(lsof -ti :3000)

That finds and stops whatever is on port 3000 in a single command. Add -9 if you need to force it:

kill -9 $(lsof -ti :3000)

Check before you kill

The one habit worth keeping: glance at the process name first, from the plain lsof -i :PORT output, so you are stopping a stray dev server and not something that matters. Ninety percent of the time it is your own leftover process, but the ten-second check saves you from the tenth case.

Why this beats the alternatives

The lazy fixes, rebooting, or changing your app to a different port, either waste minutes or paper over a process that is still running and will bite you again. Finding and stopping the actual process takes seconds and leaves your machine clean. Learn the one-liner and "port already in use" stops being a thing that interrupts you.

FAQ

What does 'port already in use' mean?

Another process is already listening on the port your app wants. Usually it is a previous run of your own server that did not shut down, or another tool using the same port. You need to find and stop that process, or use a different port.

How do I find what is using a port?

Use lsof -i :PORT on macOS and Linux to list the process holding it, including its PID and name. That tells you what to stop, and often reveals it is a stray copy of your own dev server.

Is it safe to kill the process?

If it is a stray dev server or a tool you recognise, yes. Check the process name first so you are not killing something important. Prefer a normal kill before a forced one, to let the process shut down cleanly.