Tarik Hamilton.

A full-stack hauman's space on the web.

Kill Process by Port Number in Terminal on Mac

Wednesday, November 30, 2022

If you close your Terminal window in the wrong way or it crashed, there are instances where your local server process is still running which prevent you from using the same port. Before you restart your computer or start killing off processes in Activity Manager, try this out:

List open files lsof, filtering on internet addresses -i :<PORT>.

lsof - i :3000

From the list, look for the command that's running your server. For me, 99% of the time it'll be node. Get the corresponding <PID> from the list and kill it.

kill 420

By default, kill will gracefully try to end the process using the -15 flag, but if the process doesn't die, you can use the -9 flag to kill it immediately.

kill -9 420

Scripted Version

I found a script on this "Members Only" 🙄 Medium post, which I think is the way to go for convenience, but I also feel this is simple enough to perform without a script, this way you aren't dependent on non-native scripts.

I think it could be a good exercise in scripting to make your own. His script isn't explained, so I'm wondering if the temporary text file is necessary. It also uses -9 by default.

Credit

This is a glorified re-posting of a Stack Overflow question. I've just searched this so many times, I wanted my own personal reference. 🙂