765
votes

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

For example: localhost:8080

24
npx kill-port 8080Rishabh Agarwal
npx kill-port 8080 is working for me alsoDeepak
apparently, npx kill-port is either not working at all or can't kill processes that have elevated privileges, for Windows 10.Pavindu

24 Answers

1732
votes

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.

167
votes

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

133
votes

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

109
votes

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

106
votes

If you are using GitBash

Step one:

netstat -ano | findstr :8080

Step two:

taskkill /PID typeyourPIDhere /F 

(/F forcefully terminates the process)

58
votes

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
44
votes

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)
31
votes

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

31
votes

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
30
votes

Simple CMD is working me. Easy to remember

find the port number which you want kill and run the below cmd

npx kill-port 8080

After complete the Port get stopped and getting this message

npx: installed 3 in 13.796s
Process on port 8080 killed
20
votes

Open command prompt and issue below command

netstat -ano|findstr "PID :8888"

Output will show the process id occupying the port

enter image description here

Issue below command to kill the PID

taskkill /pid 8912 /f

You will receive the output as below

SUCCESS: The process with PID 8860 has been terminated.
15
votes

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

Enter image description here

13
votes

If you can use PowerShell on Windows you just need :

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

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.
8
votes

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.

7
votes

Run cmd as administrator. Then type this code in there.

netstat -ano | findstr :<8080>

Then you can see the PID run on your port. Then copy that PID number. ( PID is a unique number that helps identify a hardware product or a registered software product.) And type below code line and press enter.

taskkill /PID <Enter the copied PID Number> /F
6
votes

Let's Automate!

If you fall into this issue much often like me, make an .bat file and run it to end process.

create a bat file "killport.bat"

set /P port="Enter port : "
echo showing process running with port %port%

netstat -ano|findstr "PID :%port%"

set /P pid="Enter PID to kill : "

taskkill /pid %pid% /f

set /P exit="Press any key to exit..."

Run this file by double clicking and

  1. Enter port number (then it will list process with PID)
  2. Enter PID to kill

Done

(Optional)

Set to path environment so that you can access this file from anywhere.

Most probably you will know how to add an new path to env. But here's how if you don't

Step 1 of 4

Search ENV on start menu

enter image description here

Step 2 of 4

Select Environment Variables

enter image description here

Step 3 of 4

Select 'path' and click Edit button

enter image description here

Step 4 of 4

Click 'New' add the path where .bat file is stored. Since I saved it on '../Documents/bats' folder I am adding this path. Your path will depend on where you save this file.

enter image description here

Open CMD and test. Remember you filename will the word to run this file. Since I saved the .bat file as 'killport.bat' => 'killport' is the word to run it.

enter image description here if do enjoy! else share how you done it here

4
votes

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
4
votes

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
4
votes

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
4
votes
netstat -ano | findstr :PORT
kill PI
2
votes

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
0
votes

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
-3
votes

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

IISRESET