Quick Command to Discover used Ports

Published: Thursday, August 20, 2020

Greetings, friends! Have you ever received an error like this?

md
Copied! ⭐️
Error: listen EADDRINUSE: address already in use :::3000

The "EADDRINUSE" error is a common error you may see when you're trying to run a server on a port that's already in use. How do you know which process is listening on this port? There's a simple command you can use to identify which process is being sneaky and stealing your resources 🤭 or maybe you have many terminal windows open and you forgot you were running a server 😅. To see what processes are listening on port 3000, you would use the following command:

bash
Copied! ⭐️
lsof -i:3000

If you run this command, your output may look something like this:

bash
Copied! ⭐️
COMMAND     PID         USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node      31561 inspirnathan   27u  IPv4 0xe073d16ee542eb1f      0t0  TCP localhost:hbci (LISTEN)
node      31561 inspirnathan   29u  IPv4 0xe073d16ed9c4cb1f      0t0  TCP localhost:hbci->localhost:63307 (ESTABLISHED)

In my example, I am running a Node server on port 3000. I see that the Node server has a process ID (PID) of 31561. If you want to stop the process from running and terminate the server, then you can use this PID and run the following command:

bash
Copied! ⭐️
kill 31561

By default, this will run the SIGTERM signal to the process. SIGTERM is a termination signal. If your process is unresponsive, you may have to be a bit more forceful and use the SIGKILL signal instead:

bash
Copied! ⭐️
kill -9 31561
warning
The SIGKILL signal will terminate a process and all its child processes as well. This could lead to what are known as "zombie processes" if you're not too careful 🧟.

Discovering used ports is very common in software development. I hope this article helps! Happy coding! 🙂