1288

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

For example: localhost:8080

Community
  • 1
  • 1
KavinduWije
  • 39,451
  • 4
  • 14
  • 17

28 Answers28

2637

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.

Mujeeb
  • 995
  • 1
  • 8
  • 18
KavinduWije
  • 39,451
  • 4
  • 14
  • 17
  • @HardikMandankaa Is it the _same_ process? Some software may be set up to relaunch the listening process if it is killed. – TripeHound Sep 22 '17 at 11:26
  • 74
    I had to enter escape characters when running taskkill: `taskkill //PID 12552 //F` – Robert Feb 15 '18 at 15:26
  • 1
    accepted answer will not work for services which are set up to restart on failure (this is not linux) – nurettin Apr 20 '18 at 10:42
  • 1
    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." – Totty.js Nov 28 '18 at 10:49
  • 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" – Chemist Jan 04 '19 at 19:47
  • What work for me is `taskkill //PID myProcessId -F` – Ashwani Panwar Jan 06 '20 at 13:01
  • 3
    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` . – VISQL Apr 03 '20 at 17:32
  • 1
    i think you meant PID_NUMBER and not PORT_NUMBER in ``taskkill /PID PORT_NUMBER /F`` – Marcin Kulik May 04 '20 at 16:16
  • with colon not working netstat -ano | findstr :, without colon working fine netstat -ano | findstr – Bharathiraja Dec 24 '20 at 05:24
  • I used : netstat -ano to get the PID number and kill the process using : taskkill /PID /F . – elakioui Aug 11 '21 at 11:47
  • 1
    what are the ano flag – Namit Piriya Aug 20 '21 at 05:35
  • 1
    I ran it in PowerShell, and `taskkill` needed to be run in an elevated shell. – mbomb007 Dec 02 '21 at 17:44
  • What should be done if netstat in Step 1 doesn't return anything, but IIS Express still complains that the port is in use? – ryanwebjackson Sep 09 '22 at 20:01
  • What's the problem @H Nazmul Hassan? run it as **administrator** – jwa Jul 10 '23 at 01:42
  • 1
    You can also use "-" instead of "/". You don't have to escape the slashes and for me "-" is more intuitive – Jonas Both Aug 18 '23 at 10:26
503

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

Requirements: npm@5.2.0^ version

npx kill-port 8080

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

Rafał Figura
  • 5,428
  • 1
  • 15
  • 20
211

There are two ways to kill the processes

Option 01 - Simplest and easiest

Requirement : npm@5.2.0^ 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
Niroshan Ratnayake
  • 3,433
  • 3
  • 20
  • 18
190

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

Community
  • 1
  • 1
afsarkhan10182
  • 2,172
  • 1
  • 13
  • 17
179

With Windows 10/11 default tools:

✔ Step one:

Open Windows PowerShell as Administrator

✔ Step two:

Find the ProcessID for the port you need to kill (e.g. 3000)

netstat -aon | findstr 3000

TCP 0.0.0.0:3000 LISTEN 1234

✔ Step three:

Kill the zombie process:

taskkill /f /pid 1234

where "1234" is your ProcessID (aka PID)

*Extra tip if you use Windows Subsystem for Linux (Ubuntu WSL):

✔ Step one:

sudo lsof -t -i:3000

✔ Step two:

sudo kill -9 1234
Java bee
  • 2,522
  • 1
  • 12
  • 25
  • 2
    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? – Serob_b Sep 20 '19 at 18:59
  • 6
    @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 – Vikrant Jan 10 '20 at 05:44
  • @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." ? – CrazyVideoGamer Jul 10 '20 at 13:51
  • 2
    @CrazyVideoGamez `@echo off` – VB_Dojnaz Jul 29 '20 at 09:36
115

If you are using GitBash

Step one:

netstat -ano | findstr :8080

Step two:

taskkill /PID typeyourPIDhere /F 

(/F forcefully terminates the process)

Uchenna Nwanyanwu
  • 3,174
  • 3
  • 35
  • 59
Ara Yaghsizian
  • 1,279
  • 1
  • 8
  • 10
  • 3
    @kylexy1357 try with a single slash. The "double slash" is an escape character that precedes the /, which is not needed for some shells – Robert Feb 15 '18 at 15:30
89

The simplest solution — the only one I can ever remember:

In Windows Powershell

Say we want to stop a process on port 8080

  1. Get the process:
netstat -ano | findstr :8080
  1. Stop the process
stop-process 82932
Luke Garrigan
  • 4,571
  • 1
  • 21
  • 29
59

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)
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
42

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
marvedly
  • 53
  • 5
Mahesh Narwade
  • 609
  • 6
  • 7
  • 1
    If you want to do it in a .bat, replace %a for %%a – Mahesh Narwade Jun 29 '18 at 10:10
  • 1
    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. – tscpp Jun 29 '20 at 07:15
  • Need really far scroll down to get a **"programmatic"** answer like this. BTw what is `tokens=5`? – Timo Mar 30 '21 at 17:45
  • a one-line solution that can be used in package.json scrripts without manual steps – Sh eldeeb Sep 15 '22 at 23:11
42

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
ux.engineer
  • 10,082
  • 13
  • 67
  • 112
Deepak
  • 571
  • 7
  • 19
34

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

todorm
  • 474
  • 4
  • 6
  • `'Stop-Process' is not recognized as an internal or external command,` – Green Jan 10 '18 at 20:56
  • 5
    I find that this works: `Stop-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess -Force` – morganpdx Jan 11 '18 at 07:25
  • I get the error: "Get-NetTCPConnection : No MSFT_NetTCPConnection objects found with property 'LocalPort' equal to '49610'. Verify the value of the property and retry." However, IIS Express is complaining about this port not being available. – ryanwebjackson Sep 09 '22 at 20:14
23

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.
Waqas Ahmed
  • 4,801
  • 3
  • 36
  • 45
20

If you can use PowerShell on Windows you just need :

Get-Process -Id (Get-NetTCPConnection -LocalPort "8080").OwningProcess | Stop-Process
  • 1
    I like this one-liner (even though it throws ugly messages when there is NO matching process)! – mmo Feb 16 '23 at 15:23
  • yes, you are right, but I think I have used when a port is blocked so I never tested the case when there is no matching process! anyway good point :) – Emanuele Fricano Mar 06 '23 at 14:42
16

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

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zuhair Taha
  • 2,808
  • 2
  • 35
  • 33
  • 2
    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. – FCA69 Feb 21 '22 at 12:29
11

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
driven_spider
  • 495
  • 4
  • 16
11

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Abhin Krishna KA
  • 745
  • 8
  • 13
11

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
9

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.

lazycipher
  • 340
  • 3
  • 14
  • I know this works because I tested it, I'm trying to find documentation around it, do you have any? – Dan Starns May 26 '20 at 10:40
  • @DanStarns, I've found only this so far! https://learn.microsoft.com/en-us/windows/terminal/ – lazycipher May 26 '20 at 21:38
  • 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. – Dan Starns May 27 '20 at 04:37
  • 1
    I haven't found much about this as of now. Please post if you find anything. – lazycipher May 28 '20 at 06:10
  • 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. – FSCKur Jan 22 '21 at 21:21
  • @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. – lazycipher Jan 27 '21 at 04:47
  • 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. – FSCKur Jan 28 '21 at 14:05
7

If you use powershell 7+ this worked for me. Just add this function in your $PROFILE file.

function killport([parameter(mandatory)] [string] $uport){
    if($upid = (Get-NetTCPConnection -LocalPort $uport -ErrorAction Ignore).OwningProcess){kill $upid}
}

then simply use killport 8080

or if you prefer just the command you can try this:

kill $(Get-NetTCPConnection -LocalPort 8761 -ErrorAction Ignore).OwningProcess
Deekshith Anand
  • 2,175
  • 1
  • 21
  • 24
6

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
flopcoder
  • 1,195
  • 12
  • 25
4

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kardi Teknomo
  • 1,375
  • 16
  • 24
  • This worked for a socket that uses a particular port number in Google Cloud as well! Thanks a ton – peevesy Apr 19 '20 at 01:12
3

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
ruscon
  • 77
  • 2
  • 9
  • 5
    Even if this is a correct answer. The question was asked for windows environment and your answer is invalid on a Linux system. – Pranav Raut Aug 29 '20 at 10:37
3
netstat -ano | findstr :PORT
kill PI
harun ugur
  • 1,718
  • 18
  • 18
2

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
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
1

I wrote a tiny node js script for this. Just run it like this: node killPort.js 8080 or whatever port you need to kill. Save the following to a killPort.js file:

const { exec } = require('child_process');
const fs = require(`fs`);

const port = process.argv.length > 2 ? process.argv[2] : ``;
if (!port || isNaN(port)) console.log(`port is required as an argument and has to be a number`);
else {
    exec(`netstat -ano | findstr :${port}`, (err, stdout, stderr) => {
        if (!stdout) console.log(`nobody listens on port ${port}`);
        else {
            const res = stdout.split(`\n`).map(s => s.trim());
            const pid = res.map(s => s.split(` `).pop()).filter(s => s).pop();
            console.log(`Listener of ${port} is found, its pid is ${pid}, killing it...`);
            exec(`taskkill /PID ${pid} /F`, (err, stdout, stderr) => {
                if (!stdout) console.log(`we tried to kill it, but not sure about the result, please run me again`);
                else console.log(stdout);
            })
        }
    });
}
shal
  • 2,854
  • 4
  • 20
  • 31
1

I am using GitBash and I error like below when ran

taskkill //PID XXXX

ERROR: The process with PID 7420 could not be terminated. Reason: This process can only be terminated forcefully (with /F option).

So used //F like below and worked fine

taskkill //F //PID XXXX

Vatsal Shah
  • 1,334
  • 18
  • 21
0

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
MyrionSC2
  • 1,248
  • 1
  • 14
  • 24
-3

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

IISRESET
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ksrider 148
  • 55
  • 1
  • 11