Kill a Process by Looking up the Port being used by it from a .BAT
Solution 1
Here's a command to get you started:
FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO @ECHO TaskKill.exe /PID %%P
When you're confident in your batch file, remove @ECHO
.
FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO TaskKill.exe /PID %%P
Note that you might need to change this slightly for different OS's. For example, on Windows 7 you might need tokens=5
instead of tokens=4
.
How this works
FOR /F ... %variable IN ('command') DO otherCommand %variable...
This lets you execute command
, and loop over its output. Each line will be stuffed into %variable
, and can be expanded out in otherCommand
as many times as you like, wherever you like. %variable
in actual use can only have a single-letter name, e.g. %V
.
"tokens=4 delims= "
This lets you split up each line by whitespace, and take the 4th chunk in that line, and stuffs it into %variable
(in our case, %%P
). delims
looks empty, but that extra space is actually significant.
netstat -a -n -o
Just run it and find out. According to the command line help, it "Displays all connections and listening ports.", "Displays addresses and port numbers in numerical form.", and "Displays the owning process ID associated with each connection.". I just used these options since someone else suggested it, and it happened to work :)
^|
This takes the output of the first command or program (netstat
) and passes it onto a second command program (findstr
). If you were using this directly on the command line, instead of inside a command string, you would use |
instead of ^|
.
findstr :8080
This filters any output that is passed into it, returning only lines that contain :8080
.
TaskKill.exe /PID <value>
This kills a running task, using the process ID.
%%P instead of %P
This is required in batch files. If you did this on the command prompt, you would use %P
instead.
Solution 2
Open command prompt and run the following commands
C:\Users\username>netstat -o -n -a | findstr 0.0:3000
TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING 3116
C:\Users\username>taskkill /F /PID 3116
, here 3116 is the process ID
Solution 3
To find specific process on command line use below command here 8080 is port used by process
netstat -ano | findstr 8080
to kill process use below command here 21424 is process id
taskkill /pid 21424 /F
Solution 4
Using Merlyn's solution caused other applications to be killed like firefox. These processes were using the same port, but not as a listener:
eg:
netstat -a -n -o | findstr :8085
TCP 0.0.0.0:8085 0.0.0.0:0 LISTENING 6568
TCP 127.0.0.1:49616 127.0.0.1:8085 TIME_WAIT 0
TCP 127.0.0.1:49618 127.0.0.1:8085 TIME_WAIT 0
Therefore, can excluded these by adding "LISTENING" to the findstr as follows:
FOR /F "tokens=5 delims= " %%P IN ('netstat -a -n -o ^| findstr :8085.*LISTENING') DO TaskKill.exe /PID %%P
Solution 5
To list all the process running on port 8080 do the following.
netstat -ano | find "8080"
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 10612
TCP [::]:8080 [::]:0 LISTENING 10612
Then to kill the process run the following command
taskkill /F /PID 10612
Mike Flynn
I am the founder and CEO of Exposure Events. Exposure Events is a tournament and league management system delivering online scheduling and conflict checker, live results, apps, free directory and more.
Updated on July 08, 2022Comments
-
Mike Flynn almost 2 years
In Windows what can look for port 8080 and try to kill the process it is using through a .BAT file?
-
Merlyn Morgan-Graham almost 13 yearsYou may need to play with tokens, delims, etc. Check
HELP FOR
on the command line to see a lot of other options thatFOR
will give you, and checknetstat -?
,findstr /?
, andTaskKill /?
for even more help. -
Mike Flynn almost 13 yearsI will try it out and get back to you.
-
Mike Flynn almost 13 yearsFOR /F "tokens=5 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO TaskKill.exe /F /PID %%P
-
Merlyn Morgan-Graham almost 13 years@Mike: Weird. tokens=4 works for me and 5 doesn't. I am thinking there is system or environment dependent stuff going on here, so be sure to watch out for that.
-
Strelok over 12 yearsThis is great.
tokens=4
is Windows XP I think andtokens=5
Windows 7. Also a good idea to/F
force the kill. -
Mohsin Javed Cheema over 10 yearsyou helped solving my problem too, thanks for really taking time to describe each of the step. two thumbs up.
-
javatar over 9 yearsFor windows server 2008, you should set tokens=5 to get the PID
-
Dinesh Kumar P over 9 yearsHi, What is the token to be set for Windows 8 and Windows 8.1?
-
Dinesh Kumar P over 9 yearsFor Windows 8, tokens=5
-
Sebastien Dionne about 9 yearsI had to run two commands : FOR /F "tokens=5 delims= " %P IN ('netstat -a -n -o ^| findstr :8080') DO TaskKill.exe /PID %P /t /f and FOR /F "tokens=5 delims= " %P IN ('netstat -a -n -o ^| findstr :8080') DO TaskKill.exe /PID %P /f The parent were never really killed
-
nishantv over 8 years@MerlynMorgan-Graham when I am trying this command I am getting an error - 'netstat -a -n -o ^| findstr :8080' is not recognized as an internal or external command. Here is what i have tried to troubleshoot: (1) The command netstat -a -n -o ^| findstr :8080 gives the result (2) I have tried replacing single quote ' with a backtick ` , but that doesn't seem to work as well. Could you please tell me what could be the problem. I am running the command on windows 7 cmd.exe
-
Merlyn Morgan-Graham over 8 years@nishantv: Back tick isn't a Windows thing, so it of course won't work. Try typing
netstat
and try typingfindstr
separately. If either of them don't work you've found your culprit. Either way, I have no idea why you don't have such executables on your box - those, as far as I know, come with Windows. You'd need to google or ask a separate question on super user to solve such a problem. If both of them work, then you either have some sort of strange escaping options configured on your terminal/prompt (enable delayed expansion? I'm not sure here) or you have a typo in your command somewhere -
Mohit Singh over 8 yearsyeah that will differ everywhere
-
Leeeeeeelo over 7 yearsvery useful ! Mind you I search for :3000 instead of 0.0:3000 because it could under 127.0.01 instead of 0.0.0.0
-
bahrep over 7 yearsThis is not really a PowerShell script, but a PowerShell-based wrapper around
netstat
tool. -
danjah over 7 yearsThank you, great job killing an out of control webpack dev task :)
-
Hooli over 7 years
Cannot run program "FOR": CreateProcess error=2, The system cannot find the file specified
-
Merlyn Morgan-Graham over 7 years@Hooli This is not a typical error to have inside a DOS Batch file. You may want to open a separate question.
-
Girdhar Singh Rathore almost 7 years@EralpB Enjoy :)
-
Mohit Singh almost 5 yearsOr you may simply provide netstat -ano | findstr :9797 taskkill /PID typeyourPIDhere /F Here 9797 is the port number, iI frequently use this command to kill the misbehaving/hanged server.
-
Sireesh Yarlagadda almost 5 years@TusharPandey This is windows tagged question , so it is a windows script, not shell script
-
IAmSurajBobade over 4 years@MerlynMorgan-Graham as this is an accepted answer, please consider adding points from stackoverflow.com/a/20637662/5243762 this answer as well
-
saurabh.in over 4 yearsI had to remove %%P with %P or else was gettin this error: "%%P was unexpected at this time."
-
Merlyn Morgan-Graham over 4 years@saurabh.in I already stated this at the very bottom of my answer, so make sure to read the entire answer before commenting.
-
Jakob over 4 yearsThis is dependent on the language of the OS, any way to make it work for all languages?
-
Eduard Florinescu over 4 years@Jakob This can only be run in cmd terminal in Windows, also only in .cmd or .bat files
-
Jakob over 4 yearsYes, but only in english language, since otherwise "LISTENING" will be translated to the language of the OS.
-
Eduard Florinescu over 4 years@Jakob I am not aware that
netstat
tool is translated to other languages -
Nikola Lukic almost 3 yearsI got
%%P was unexpected at this time.
Any suggestion ? -
Nikola Lukic almost 3 yearsWorks fine on windows 10 !