Kill server process on port

First published: January 23, 2020 (6y ago)

how todev-ops

I've lost track of how many times I've needed to terminate a process that's using a particular port on my macOS or Linux machine.

Maybe I'm running a Severless function locally and accidentally create an infinite loop, or using a poorly constructed database seed that never finishes, or my IDE's integrated terminal crashed.

Whatever the cause the result is the same; I have a process running in the background that's blocking one of my ports.

Here's the command I use to find the ID of that process and terminate it:

lsof -nti:NumberOfPort | xargs kill -9

Where NumberOfPort is replaced with the port number you're looking for.

Warning

Killing a process in this manner does not allow it to close files or database connections cleanly and over time could cause other issues; therefore it is generally better to reserve this approach as a last resort.

Is a process hogging a port on your macOS or Linux machine?

The form below will generate the command to find the ID of the process and terminate it, then copy the command to your clipboard.

Once you've copied the command, open your Terminal app, paste the command, and hit the return / enter key.