How To Kill/Terminate process that Work On a Specific Port on Windows/MacOS/Linux

As software developers, we are constantly developing new projects on our computer. While developing our Backend or Frontend projects, we run these projects on a port of our computer and in this way we can test our project.

Sometimes, while running our projects on a local server, the port where we will host our application may be already in use. This requires us to change the configuration of our application and assign a new port address. Of course, this is also an option, but in this article, we will learn how to terminate the process in the already occupied port.

For Linux Users

First of all, I will explain step by step how we can terminate a process running on a port for linux users. Linux users can terminate a process running on a specific port using a single command line.

    kill -9 $(lsof -t -i tcp:<port>)

In this command you should replace <port> with port number that you want to kill/terminate. In addition, sometime you might have to add sudo command to beginning of this command.

For MacOS Users

In macOs you need to execute two commands. In first command you will get a PID value for specific port that you wanna kill/terminate. For this you need to execute this command on your terminal.

    sudo lsof -i :<port>

Again in this command you need to replace <port> with port number that you want to kill/terminate. After executing this command you will get a PID value. Copy or store this PID value to somewhere to use in the next command.

In second command we will terminate specific port with PID value that store earlier. To do this execute below command.

    kill -9 <PID>

Again replace <PID> with PID value that we get from previous command. After executing this command process that working in that specific port will be terminated immidiately.

Note: Sometimes we do not want to terminate process that working on a specific port immidiately. In this command -9 argument describe instant termination. And this argument kills the process immediately, and gives it no chance of cleaning up after itself, and this may cause problems. Use it on your own risk. You can consider using -15 or -3 for a softer termination which allows the process to clean up after itself.

For Windows Users

These couple of steps are almost same for Windows users. To terminate a process on a specific port on windows, we need to first find the id of that process and then terminate that process.

We can use Windows CMD, Terminal or Powershell for these operations. You can use any of these. First open one of these. And then write this command.

    netstat -ano | findstr :<PORT>

Similar to before, you need to replace <PORT> with port number that you want to terminate. After executing this command you will get a list of process that working on that specific port. The list item has an id value on the far right. Copy that id value and use this value in the next command to terminate a process.

    taskkill /PID <PID> /F

Again use that id value that we copied in the previous step. And replace it with <PID> in this command. After executing this command, you will terminate the working process on that specific port.

Basically, I tried to explain how we can stop processes running in various operating systems. Thanks for reading this far. I hope this article was helpful. See you next time…


İsa

Software Engineer