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

1,977,545

Solution 1

Step 1:

Open up cmd.exe (note: you may need to run it as an administrator, but this isn't always necessary), then run the below command:

netstat -ano | findstr :<PORT>

(Replace <PORT> with the port number you want, but keep the colon)

The area circled in red shows the PID (process identifier). Locate the PID of the process that's using the port you want.

Step 2:

Next, run the following command:

taskkill /PID <PID> /F

(No colon this time)

Lastly, you can check whether the operation succeeded or not by re-running the command in "Step 1". If it was successful you shouldn't see any more search results for that port number.

Solution 2

I know that is really old question, but found pretty easy to remember, fast command to kill apps 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

Solution 3

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

Solution 4

There are two ways to kill the processes

Option 01 - Simplest and easiest

Requirement : [email protected]^ version

Open the Command prompt as Administrator and give the following command with the port (Here the port is 8080)

npx kill-port 8080

Option 02 - Most commonly used

  • Step 01

    Open Windows command prompt as Administrator
  • Step 02

    Find the PID of the port you want to kill with the below command: Here port is 8080
netstat -ano|findstr "PID :8080"

TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 18264

  • Step 03

    Kill the PID you received above with the below command (In my case PID is 18264)
taskkill /PID 18264 /f

Solution 5

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

Share:
1,977,545
KavinduWije
Author by

KavinduWije

Updated on February 02, 2022

Comments

  • KavinduWije
    KavinduWije over 2 years

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

    For example: localhost:8080

  • TripeHound
    TripeHound over 6 years
    @HardikMandankaa Is it the same process? Some software may be set up to relaunch the listening process if it is killed.
  • Green
    Green over 6 years
    'Stop-Process' is not recognized as an internal or external command,
  • morganpdx
    morganpdx over 6 years
    I find that this works: Stop-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess -Force
  • Robert
    Robert about 6 years
    I had to enter escape characters when running taskkill: taskkill //PID 12552 //F
  • Robert
    Robert about 6 years
    @kylexy1357 try with a single slash. The "double slash" is an escape character that precedes the /, which is not needed for some shells
  • nurettin
    nurettin about 6 years
    accepted answer will not work for services which are set up to restart on failure (this is not linux)
  • dxhans5
    dxhans5 almost 6 years
    This is the solution that worked for me. I'm using GIT Bash.
  • Mahesh Narwade
    Mahesh Narwade almost 6 years
    If you want to do it in a .bat, replace %a for %%a
  • Totty.js
    Totty.js over 5 years
    I got processId 0 on port 80. If I try to kill it I get "ERROR: The process with PID 0 could not be terminated. Reason: This is critical system process. Taskkill cannot end this process."
  • Chemist
    Chemist over 5 years
    The colon symbol has to be in a very specific place. Can't change spaces or this command won't work. Otherwise this also works without colon: "netstat -ano | findstr 8080"
  • vhs
    vhs about 5 years
    If you know the port is using IPv4 you can do an lsof -nt -i4TCP:9001 as well.
  • Serob_b
    Serob_b over 4 years
    How to automate the process and combine these two commands together in one bat file if there is no possibility to view the cmd output and then write new command manually?
  • Ashwani Panwar
    Ashwani Panwar over 4 years
    What work for me is taskkill //PID myProcessId -F
  • Vikrant
    Vikrant over 4 years
    @Serob_b set /p port="Enter port: " -> Input port FOR /F "tokens=5" %%T IN ('netstat -aon ^| findstr %port% ') DO ( SET /A ProcessId=%%T) &GOTO SkipLine :SkipLine -> Extracts PID into variable taskkill /f /pid %ProcessId% -> Kills the task cmd /k -> Keep the window open
  • VISQL
    VISQL about 4 years
    2020-04-03, Windows10, Python 3.6.6, using Git Bash: confirming that using the double forward slash worked for me as well. I needed to kill a Flask app process on localhost:5000 that didn't terminate. e.g. taskkill //F //PID 16660 .
  • peevesy
    peevesy about 4 years
    This worked for a socket that uses a particular port number in Google Cloud as well! Thanks a ton
  • Marcin Kulik
    Marcin Kulik about 4 years
    i think you meant PID_NUMBER and not PORT_NUMBER in taskkill /PID PORT_NUMBER /F
  • Dan Starns
    Dan Starns almost 4 years
    I know this works because I tested it, I'm trying to find documentation around it, do you have any?
  • lazycipher
    lazycipher almost 4 years
    @DanStarns, I've found only this so far! docs.microsoft.com/en-us/windows/terminal
  • Dan Starns
    Dan Starns almost 4 years
    Thanks for your time, it's not quite the page I'm looking for, a page with all the functions provided in the terminal such as kill. Ill post here if i find.
  • lazycipher
    lazycipher almost 4 years
    I haven't found much about this as of now. Please post if you find anything.
  • tscpp
    tscpp almost 4 years
    This answer was the only one line command to work for me so it's really good. I have a task running on two ports which cause the error "ERROR: The process "6552" not found." because the task has already been exited.
  • Michael Elliott
    Michael Elliott almost 4 years
    I can't believe I didn't know about this, so useful thank you!
  • CrazyVideoGamer
    CrazyVideoGamer almost 4 years
    @Vikrant Is there a way to hide all the commands and just show "Enter port: " and "SUCCESS: The process with PID 35264 has been terminated." ?
  • gunes
    gunes almost 4 years
    I had to do kill -9 $(lsof -t -i :PORT_NUMBER)
  • VB_Dojnaz
    VB_Dojnaz almost 4 years
    @CrazyVideoGamez @echo off
  • Pranav Raut
    Pranav Raut over 3 years
    Even if this is a correct answer. The question was asked for windows environment and your answer is invalid on a Linux system.
  • Eugene Beliaev
    Eugene Beliaev over 3 years
    yayy, awesome way :)
  • gatsbyz
    gatsbyz over 3 years
    love u so much rn
  • Bharathiraja
    Bharathiraja over 3 years
    with colon not working netstat -ano | findstr :<portNumber>, without colon working fine netstat -ano | findstr <portNumber>
  • FSCKur
    FSCKur over 3 years
    I have to downvote because of your confusion about kill. It's NOTHING to do with your choice of terminal. As any fool knows, in PS you use Get-Command to understand what command you're running, and it shows you that, on Windows, kill is an alias for Stop-Process. On Linux it's not an alias but the native command.
  • lazycipher
    lazycipher over 3 years
    @FSCKur, I don't think it was already there earlier(talking about alias)! It's something new that I found. Thanks for letting me know about the alias thing. I'll mention this thing in the answer.
  • FSCKur
    FSCKur over 3 years
    Appreciated; sorry about my previous sharp tone. However, there's still confusion between terminal and shell. You have provided a Powershell answer. A user running bash in WT wouldn't get the same results. If you change "WT" to "PS" then your answer becomes correct... however, it's a PS community guideline to not use aliases in code or help, so I'd still not be happy.
  • Shardj
    Shardj about 3 years
    Thank you, so much overkill in other answers. Literally just need this scripted
  • Timo
    Timo about 3 years
    Need really far scroll down to get a "programmatic" answer like this. BTw what is tokens=5?
  • Basit
    Basit almost 3 years
    Love it, Thanks for adding up in my knowledge
  • Admin
    Admin almost 3 years
    Thanks a lot, save my day!
  • elakioui
    elakioui over 2 years
    I used : netstat -ano to get the PID number and kill the process using : taskkill /PID <PID> /F .
  • Code Whisperer
    Code Whisperer over 2 years
    Unnecessary comment but huzzah!
  • GorvGoyl
    GorvGoyl over 2 years
    The term 'lsof' is not recognized as a name of a cmdlet, function, script file
  • Namit Piriya
    Namit Piriya over 2 years
    what are the ano flag
  • Daniel
    Daniel over 2 years
    I can't believe I never scrolled down to see this... I have come to this SO page so many times when I forget the bash command... This is so much easier to remember
  • Chau Giang
    Chau Giang over 2 years
    I got the error: bash: taskill: command not found
  • Italo Borssatto
    Italo Borssatto over 2 years
    @ChauGiang It seems a typo. Use tskill instead of taskill.
  • sg28
    sg28 over 2 years
    Worked like a Charm for killing the running port. Thank you
  • Rahul
    Rahul over 2 years
    Awesome, Hurrey
  • mbomb007
    mbomb007 over 2 years
    I ran it in PowerShell, and taskkill needed to be run in an elevated shell.
  • Code Cooker
    Code Cooker over 2 years
    this one helped me! I'm on Win10 ~
  • FCA69
    FCA69 about 2 years
    No need for CurrPorts. In windows 10+ you can use the Resource Monitor, more precisely its Network Tab. In the bottom pane, there is a section listing all the opened ports and the PID of the processes which are using these ports.
  • Cos
    Cos almost 2 years
    Where is the $PROFILE file?