Find and Kill a Process on a Specific Port on macOS

March 26, 2020

macos

TLDR

bash
1.sudo lsof -i :<PortNumber>
2.
3.kill -9 <PID>

Find out what is running on a particular port on macOS

You can find out what is running on a specific port by running the command lsof -i with the port number, :<PortNumber>.

bash
1.sudo lsof -i :<PortNumber>

Example

bash
1.> sudo lsof -i :8000
2.
3.COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
4.node 24922 cody 79u IPv4 0x4d990156241107c9 0t0 TCP localhost:irdmi->localhost:60453 (ESTABLISHED)

Kill the process running on a specific port on macOS

We can use the kill command with the -9 option and the port PID number to kill a process on macOS. Use the command above, lsof -ito find the PID for the port you want to kill.

bash
1.kill -9 <PID>

Example

In the above example there is a process on port 8000 with a PID that equals 24922. So we can kill that process using the following command:

bash
1.kill -9 24922

Which won't have any output, but if you were running a server on that port in another terminal window then you can usually see that the process was killed.

bash
1.zsh: killed npm start