Mac OSX

How to find and kill process locking port on macOS

Certain processes use specific TCP/UDP ports which in return locks that port from being accessed with other applications. Sometimes it becomes necessary to unlock a port on macOS for another important application which uses the same port. If you are using macOS for mysql, apache or rails server then probably you’ll come across this problem a lot more than usual. The only way forward is to kill process port and unlock it. Most errors happen with port 3000 or port 8080. It’s important to note that HTTP uses port 80, FTP uses port 21, so don’t close these important processes if you are using macOS server.

Find out running application on the port

As we all know that port is a virtual point where a connection starts and culminates. If a certain port on macOS is busy and the concerned application is throwing this error:

Port already in use, can’t bind.

Then you’ve to make sure that you find out the application which is already using that particular port. Use the following command in terminal:

  • sudo lsof -i :PORT_NUMBER

Incase the output is empty then it means the port is not in use. Make sure to replace PORT_NUMBER with the relevant port number such as 8000 etc. In order to list all the ports in use, you can try simple lsof -i (list open files) command. Make sure to note the PID of the process which is running on the port already in use.

Kill process running on specific port

You need to kill the process using terminal through PID. We’ve already covered the topic with Activity Monitor and Terminal. Incase you’ve missed the article then use a simple following command with the noted PID to kill the process which is keeping the port locked.

  • kill -9 PID_NUMBER

Make sure to replace PID with the noted one in the first step where you used lsof command to trace the port keeping it busy. You’ve to run all these commands on macOS terminal.

Alternatively, you can use following command to directly kill a process running on port 3000:

  • kill $(lsof -t -i:8080)

Conclusion:

The lsof command (list open files) with an argument -i helps you trace all the open files of the network, giving a list of their PID as well as port numbers. This in return can help you kill a process locking a specific port on macOS. As a result, you can easily start another communication on the same port. One thing must be kept in mind, not to close an important application/process which is mandatory. Instead, try to assign another port number to the application which you are trying to bind with an already locked port. This can be done by editing configuration files.