Kill process running on port

Frequently I’ll run into the case where my IDE (Intellij, Xcode/Vapor) will fail and leave a web server running on a port. I can’t start it again because it is already running. The only way to proceed is to restart your computer or find and kill the process.

To find the a process running on a port you can use the lsof command. You can filter with “-i”, which lists IP sockets.

$ lsof -i tcp:8080

The command will produce output like:

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
Run 8299 ddiego 15u IPv6 0xb2782dc730d0c751 0t0 TCP localhost:http-alt (LISTEN)

The PID is the process identification number. You can pass this to the kill command to kill the process:

$ kill -9 8299