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?
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
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
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.
"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,
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
This filters any output that is passed into it, returning only lines that contain
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
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
Read more... Read less...
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
Using Merlyn's solution caused other applications to be killed like firefox. These processes were using the same port, but not as a listener:
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
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
Thank you all, just to add that some process wont close unless the /F force switch is also send with TaskKill. Also with /T switch, all secondary threads of the process will be closed.
C:\>FOR /F "tokens=5 delims= " %P IN ('netstat -a -n -o ^| findstr :2002') DO TaskKill.exe /PID %P /T /F
For services it will be necessary to get the name of the service and execute:
sc stop ServiceName
Created a bat file with the below contents, it accepts the input for port number
@ECHO ON set /p portid=Enter the Port to be killed: echo %portid% FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr %portid% ') DO ( SET /A ProcessId=%%T) &GOTO SkipLine :SkipLine echo ProcessId to kill = %ProcessId% taskkill /f /pid %ProcessId% PAUSE
Finally click "Enter" to exit.