How to Kill/terminate a Process on a Specific Port in CMD on Windows

How to Kill/terminate a Process on a Specific Port in CMD on Windows

This tutorial is strictly for Windows users, Mac & Linux users will have another separate one

Introduction

Picture this: You're dealing with a crashed application on your Windows PC. The app is down, but somehow it doesn't leave the port it was using, despite no longer doing anything useful with it. This lingering process occupies the port so stubbornly that it acts as a roadblock for other applications that might want to use the same port. This kind of scenario can be frustrating, especially, when you are trying to start a new process on that same port but keep bumping into this invisible blockade. I had the same scenario yesterday when running though a project with Strapi and when I solved it, I was like why not write about this. In this blog post, I'll guide you through how to Kill or terminate such processes running on a particular port, freeing up your desired port for use again.

Steps to Kill a Process on a Specific Port

Step 1: Open Command Prompt

Let's put on our techy hats and get started! The first step is to open a Command Prompt. If you are unsure about how to do it, simply press the 'Win + R' keys, a 'Run' dialogue box will appear. In the run box, type in 'cmd', then hit 'Enter'. Voila! You're in the Command Prompt. This simple black window is a gateway to the behind-the-scenes operations on your Windows PC!

Step 2: Run the netstat Command

Now that you have command prompt open, it's time to dive a touch deeper. Input the following command and replace 'yourPortNumber' with the actual port number you're interested in (for example, 3000 if it's the one that is occupied):

netstat -ano | findstr :yourPortNumber

So, what did this seemingly complex command do? It asked Windows to list out the current network connections and filter the output to display only the line of data containing your specified port number.

Step 3: Identify the Process ID (PID)

Take a look at the resulting output from the netstat command. Amongst different columns, it represents crucial information about active connections including the local and foreign addresses, state of connections, and the Process ID (PID). The PID you need is the numerical string at the end of the line. The PID is like a unique identity tag for each process; with this ID, we can zero in on the pesky lingering process that we want to eliminate.

Step 4: Use the taskkill Command

Armed with the rogue process id (PID), it's high time to directly deal with that obstinate process. Go ahead and input the 'taskkill' command with the PID you've just found:

taskkill /PID typeyourPIDhere /F

Please note, replace 'typeyourPIDhere' with your actual PID. The '/F' flag at the end of the command means you're asking Windows to forcefully stop the process. Once you hit the 'Enter' key, you've completed the mission! The process running on your desired port is no longer in the picture.

As a caveat, handle this process with care. Make sure you're only stopping processes that you know for sure are causing an issue, and not harming important system processes. The steps above are easy to follow and provide a simple yet effective solution to remove a lingering process on a specific port in Windows. Whether you're a savvy developer needing an occupied port for your new app or just a regular user experiencing issues after a crash, these steps should guide you to free up your port and get things running smoothly again. Happy coding!

Did you find this article valuable?

Support Were Samson Bruno by becoming a sponsor. Any amount is appreciated!