Using lsof and ps to Find Running Services

Sometimes when running code locally, you may see an error with:

Error: listen EADDRINUSE: address already i use 127.0.0.1:4567

To find what's using it (and potentially stop it), you can use a combination of the lsof and ps commands.

First, use lsof -P -i :<PORT> replacing <PORT> with the port you want to check.

Using -P with the lsof flag will keep you from being confused by the NAME column containing common services that run on those ports (port 4567 for bcm-reporting and 4568 for tram for example).

Take the PID from lsof and run ps <PID> to find what's running on this port. If it's okay to kill it, run kill <PID> and try to re-run your command again.

If you're interested in seeing what service MacOS expects to be serving on a specific port, you can run the following command and replace 4567 with the port you're curious about:

 cat /etc/services | grep " 4567/tcp"
Did you enjoy this article? Share it on Twitter!
Tags