Kill a Process by Looking up the Port being used by it from a .BAT
In Windows what can look for port 8080 and try to kill the process it is using through a .BAT file?
Solution 1:
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 2:
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 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