Member-only story
Is there a way to taskkill all process ID’s that are using a specific port?
A one liner that saved my life
During my coding life I have had some issues with port’s not unbinding when a task was supposed to finish. To solve this I asked ChatGPT to give me a one liner to kill all tasks that are using a specific port. Here is what I got:
Yes, you can use the command line to taskkill all process IDs that are using a specific port. Here are the steps:
Open the command prompt as an administrator.
Type the following command to find the process IDs (PIDs) that are using the specific port. Replace [port_number] with the port number you want to check:
netstat -ano | findstr :[port_number]
This command will display a list of all the processes that are using the specified port, along with their PIDs.
Use the taskkill command to terminate the processes using the PIDs you found in step 2. Replace [PID] with the PID of the process you want to terminate:
taskkill /F /PID [PID]
You can repeat this command for each PID that is using the specified port until all of the processes are terminated.
Alternatively, you can use a single command that combines steps 2 and 3:
for /f "tokens=5" %a in ('netstat -ano ^| findstr :[port_number]') do taskkill /f /pid %a
This command will find all processes using the specified port and automatically terminate them. Note that you should replace [port_number] with the actual port number you want to check.