Use lsof and ps to Find Running Services
Essays and updates on product, engineering, and AI by Chase Adams.
1 minute read
Sometimes when attempting to run a NodeJS server locally, you may see an error with:
Error: listen EADDRINUSE: address already i use 127.0.0.1:4567
To determine what's listening on this port (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.
We use the -P with lsof since we likely don't need the mapping for the port to a name in the network files1.
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.
Footnotes
-
By using
lsof -P, it ignores the host machines conversion of port numbers to port names for network files. For example, if we ranlsofwithout-P, theNAMEcolumn would contain services that run on those ports, such as port 4567 forbcm-reportingor 4568 fortram. If you're interested in seeing what service MacOS expects to be serving on a specific port, you can run the following command and replace4567with the port you're curious about:cat /etc/services | grep " 4567/tcp". ↩
Build Your Website with AI—No Code Required
Learn to create and deploy professional websites using ChatGPT and Claude. Go from complete beginner to confident website builder.
