DEV Community

Cover image for How to close a port in use on Windows
Danilo Assis
Danilo Assis

Posted on

How to close a port in use on Windows

Several times there was a task in my simple day-to-day life that often took me five minutes to remember the commands to execute it at the prompt, or a few more minutes to do a quick google search and remember the process as a whole.

Thinking that maybe some more people can go through the same simple stress, combining with the idea that knowledge does not occupy space and with the old desire that I have to write for you, I decided to write this short post that can help several people, just like me and use him as a pilot here in the medium.

Problem
Daily running the famous yarn start, between one project and another a door was in use and I wasn't even using it anymore. Because of that, I was going to remember the process that consisted of opening the cmd, setting some commands, taking the PID of the process in question and then ending it. Of course, there must be simpler ways to do the same, but at the moment I follow this and never remember it in my head. So below I will list how to do this role in a practical and quick way:

  • Let's start by opening the command prompt by running it as a system administrator.
  • After that go ahead and type in it netstat -a -n -o

Image showing how the processes are listed.

Image showing how the processes are listed.

Used with the a argument, the netstat command shows the set of connections and ports being listened to on the computer.

Used with the n. Argument, the netstat command shows addresses and port numbers in digital format, without name resolution(pt_BR).

Used with the o argument, the netstat command details the process number associated with the connection.

  • After this we just need to identify the PID related to the connection we want to terminate, in my case it is port 9001, so we have 11348.

  • Still at the prompt we trigger the following taskkill / PID command

  • In place of we replaced it with 11348 and then the taskkill / PID 11348

  • We then receive the following information at the prompt

Prompt image stating that the process has ended.

Prompt image stating that the process has ended.

But the list of processes is very long
You may notice that when requesting the list of open cases, an extensive result of it will be displayed. There is a way to improve this command by filtering the search results by the STATE of the process. So it ends up looking like this:

  • netstat -aon | find / i “listening”

However, it is important to remember that if your windows is running in a different language, it will be necessary to change “listening” to the term referring to it.

End =]

Top comments (0)