Find And Kill Process Running On Specific Port On macOS

March 6, 2024 by Andreas Wik

Port Error

Every now and then when I’m about to start something up, maybe a local dev server, I get this annoying error telling me that the port is already being used by another process.

Example:

Error: listen EADDRINUSE: address already in use :::3001

 

So let’s see how you can find and kill the process occupying a specific port.

 

Find the process

Find the process by running the following command in the terminal:

lsof -i :<PORT_NUMBER>

You may or may not need to run it with sudo. In that case:

sudo lsof -i :<PORT_NUMBER>

If I run this command on my system right now to see what’s running on port 3001 I’ll go:

lsof -i :3001

This is the output:

COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    64575 andreas   26u  IPv6 0xdae1ec221413b353      0t0  TCP *:redwood-broker (LISTEN)

 

Kill the process

You need the PID in the output above in order to kill it. In this case it’s 64575, so I will run:

kill -9 20384

 

That should be it. If you now run lsof -i :<PORT_NUMBER> again you should get no output and the port should be free to use.

bada bing bada boom

Share this article

Recommended articles

@awikio