Finding Which Process is Locking a Port on macOS

April 7, 2020

Occasionally, a process on my computer will open a port and refuse to release it. Here's how to find the process and kill it.

lsof -i tcp:PORTNUMBER

For example, if I have a Rails server running in the background, I can run:

lsof -i tcp:3000

To find the Rails process and its PID. To kill that process, I can then run:

kill -9 PIDNUMBER

Or, to combine searching for the process and killing it into one command:

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