windows - Kill a Process by Looking up the Port being used by it from a .BAT -
in windows can port 8080 , try kill process using through .bat file?
here's command started:
for /f "tokens=4 delims= " %%p in ('netstat -a -n -o ^| findstr :8080') @echo taskkill.exe /pid %%p
when you're confident in batch file, remove @echo
.
for /f "tokens=4 delims= " %%p in ('netstat -a -n -o ^| findstr :8080') taskkill.exe /pid %%p
note might need change different os's. example, on windows 7 might need tokens=5
instead of tokens=4
.
how works
for /f ... %variable in ('command') othercommand %variable...
this lets execute command
, , loop on output. each line stuffed %variable
, , can expanded out in othercommand
many times like, wherever like. %variable
in actual use can have single-letter name, e.g. %v
.
"tokens=4 delims= "
this lets split each line whitespace, , take 4th chunk in line, , stuffs %variable
(in our case, %%p
). delims
looks empty, space significant.
netstat -a -n -o
just run , find out. according command line help, "displays connections , listening ports.", "displays addresses , port numbers in numerical form.", , "displays owning process id associated each connection.". used these options since else suggested it, , happened work :)
^|
this takes output of first command or program (netstat
) , passes onto second command program (findstr
). if using directly on command line, instead of inside command string, use |
instead of ^|
.
findstr :8080
this filters output passed it, returning lines contain :8080
.
taskkill.exe /pid <value>
this kills running task, using process id.
%%p instead of %p
this required in batch files. if did on command prompt, use %p
instead.
Comments
Post a Comment