[windows] How do I kill the process currently using a port on localhost in Windows?

How can I remove the current process/application which is already assigned to a port?

For example: localhost:8080

This question is related to windows cmd localhost port command-prompt

The answer is


One line solution using GitBash:

 tskill `netstat -ano | grep LISTENING | findstr :8080 | sed -r 's/(\s+[^\s]+){4}(.*)/\1/'`

Replace 8080 with the port your server is listening to.

If you need to use it often, try adding to your ~/.bashrc the function:

function killport() {
        tskill `netstat -ano | findstr LISTENING | findstr :$1 | sed -r 's/^(\s+[^\s]+){4}(\d*)$/\1/'`
}

and simply run

killport 8080

Result for Windows Command Prompt

In my case, 8080 is the port I want to kill
And 18264 is the PID listening on port 8080
So the task you have to kill is the PID for that particular port

C:\Users\Niroshan>netstat -ano|findstr "PID :8080"

Proto Local Address Foreign Address State PID
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 18264

taskkill /PID 18264 /f


If you're using Windows Terminal then the killing process might be little less tedious. I've been using windows terminal and kill PID works fine for me to kill processes on the port as the new Windows Terminal supports certain bash commands. For example: kill 13300

So, the complete process will look like this-

  • Open Windows Terminal
  • Type the following command to show processes running on the port you're looking to kill processes. netstat -ano | findstr :PORT
  • Type following to kill the process. kill PID

For Example:

PS C:\Users\username> netstat -ano | findstr :4445
  TCP    0.0.0.0:4445           0.0.0.0:0              LISTENING       7368
  TCP    [::]:4445              [::]:0                 LISTENING       7368
PS C:\Users\username> kill 7368
PS C:\Users\username> netstat -ano | findstr :4445
PS C:\Users\username>

See when I typed the first command to list processes on the port it returned empty. That means all processes are killed now.

Update: kill is an alias for Stop-Process. Thanks, @FSCKur for letting us know.


Step 1 (same is in accepted answer written by KavinduWije):

netstat -ano | findstr :yourPortNumber

Change in Step 2 to:

tskill typeyourPIDhere 

Note: taskkill is not working in some git bash terminal


Here is a script to do it in WSL2

PIDS=$(cmd.exe /c netstat -ano | cmd.exe /c findstr :$1 | awk '{print $5}')
for pid in $PIDS
do
    cmd.exe /c taskkill /PID $pid /F
done

If you can use PowerShell on Windows you just need :

Get-Process -Id (Get-NetTCPConnection -LocalPort "8080").OwningProcess | Stop-Process

I know that is really old question, but found pretty easy to remember, fast command to kill app that are using port.

Requirements: [email protected]^ version

npx kill-port 8080

You can also read more about kill-port here: https://www.npmjs.com/package/kill-port


netstat -ano | findstr :PORT
kill PI

I was running zookeeper on Windows and wasn't able to stop ZooKeeper running at 2181 port using zookeeper-stop.sh, so tried this double slash "//" method to taskkill. It worked

     1. netstat -ano | findstr :2181
       TCP    0.0.0.0:2181           0.0.0.0:0              LISTENING       8876
       TCP    [::]:2181              [::]:0                 LISTENING       8876

     2.taskkill //PID 8876 //F
       SUCCESS: The process with PID 8876 has been terminated.

With Windows 10 default tools:

  • Step one:

Open Windows PowerShell as Administrator

  • Step two:

Find PID (ProcessID) for port 8080:

netstat -aon | findstr 8080

TCP 0.0.0.0:8080 0.0.0.0:0 LISTEN 77777

  • Step three:

Kill the zombie process:

taskkill /f /pid 77777

where "77777" is your PID


For use in command line:

for /f "tokens=5" %a in ('netstat -aon ^| find ":8080" ^| find "LISTENING"') do taskkill /f /pid %a

For use in bat-file:

for /f "tokens=5" %%a in ('netstat -aon ^| find ":8080" ^| find "LISTENING"') do taskkill /f /pid %%a

For Windows users, you can use the CurrPorts tool to kill ports under usage easily:

Enter image description here


If you are using GitBash

Step one:

netstat -ano | findstr :8080

Step two:

taskkill /PID typeyourPIDhere /F 

(/F forcefully terminates the process)


You can do by run a bat file:

@ECHO OFF                                                                              
FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr "9797" ') DO (
SET /A ProcessId=%%T) &GOTO SkipLine                                                   
:SkipLine                                                                              
echo ProcessId to kill = %ProcessId%
taskkill /f /pid %ProcessId%
PAUSE

the first step

netstat -vanp tcp | grep 8888

example

tcp4     0      0    127.0.0.1.8888   *.*    LISTEN      131072 131072  76061    0
tcp46    0      0    *.8888           *.*    LISTEN      131072 131072  50523    0

the second step: find your PIDs and kill them

in my case

sudo kill -9 76061 50523

If you already know the port number, it will probably suffice to send a software termination signal to the process (SIGTERM):

kill $(lsof -t -i :PORT_NUMBER)

We can avoid this by simple restarting IIS, using the below command:

IISRESET

In case you want to do it using Python: check Is it possible in python to kill process that is listening on specific port, for example 8080?

The answer from Smunk works nicely. I repeat his code here:

from psutil import process_iter
from signal import SIGTERM # or SIGKILL

for proc in process_iter():
    for conns in proc.connections(kind='inet'):
        if conns.laddr.port == 8080:
            proc.send_signal(SIGTERM) # or SIGKILL
            continue

In Windows PowerShell version 1 or later to stop a process on port 3000 type:

Stop-Process (,(netstat -ano | findstr :3000).split() | foreach {$[$.length-1]}) -Force


As suggested by @morganpdx here`s a more PowerShell-ish, better version:

Stop-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess -Force


Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to cmd

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH VSCode Change Default Terminal How to install pandas from pip on windows cmd? 'ls' in CMD on Windows is not recognized Command to run a .bat file VMware Workstation and Device/Credential Guard are not compatible How do I kill the process currently using a port on localhost in Windows? how to run python files in windows command prompt?

Examples related to localhost

Xampp localhost/dashboard Set cookies for cross origin requests Invalid Host Header when ngrok tries to connect to React dev server How to turn on/off MySQL strict mode in localhost (xampp)? What is IPV6 for localhost and 0.0.0.0? How do I kill the process currently using a port on localhost in Windows? How to run html file on localhost? Can't access 127.0.0.1 Server http:/localhost:8080 requires a user name and a password. The server says: XDB How to solve ERR_CONNECTION_REFUSED when trying to connect to localhost running IISExpress - Error 502 (Cannot debug from Visual Studio)?

Examples related to port

Docker - Bind for 0.0.0.0:4000 failed: port is already allocated How do I kill the process currently using a port on localhost in Windows? Node.js Port 3000 already in use but it actually isn't? Can't connect to Postgresql on port 5432 Spring Boot - How to get the running port Make docker use IPv4 for port binding How to change the default port of mysql from 3306 to 3360 Open firewall port on CentOS 7 Unable to launch the IIS Express Web server, Failed to register URL, Access is denied XAMPP Port 80 in use by "Unable to open process" with PID 4

Examples related to command-prompt

CMD command to check connected USB devices How do I kill the process currently using a port on localhost in Windows? PowerShell The term is not recognized as cmdlet function script file or operable program open program minimized via command prompt How to connect to SQL Server from command prompt with Windows authentication How to see the proxy settings on windows? How do I type a TAB character in PowerShell? Batch file to split .csv file Aliases in Windows command prompt Change all files and folders permissions of a directory to 644/755