How to check ports in use Linux & MacOS
Sometime when trying to start server processes you may get a message to say that the port is already in use. So how do you work out what is using the port and how do you fix it?
To see which process is using the port use this command:
sudo lsof -nP -iTCP:5000 | grep LISTEN
In this case we’re looking for what is using port 5000. If anything is using the port you will see something like this:
Python 78500 bob 5u IPv4 0xdd33030855f89bcb 0t0 TCP 127.0.0.1:5000 (LISTEN)
This is saying that Python is listening in port 5000 on a process with the id, 78500.
Once you have the process ID you can kill it using the command: