windows 关闭端口被占用脚本

cmd 关闭进程java

taskkill /F /IM java.exe

taskkill /f /im java.exe

如何用dat批处理文件关闭某端口对应程序-Windows自动化命令

如何用dat批处理文件关闭某端口对应程序?

网上找到的大部分都是手动操作,第一步先查出端口,第二步在根据上一步查到的端口手动去关闭进程。但我的需求不是这样的,我需要全自动处理。用于 dubbo 服务进程在 Windows 环境下的关闭。如果 Linux 环境,则不需那么辛苦了。

找了大半天,终于在百度知道上得到参考案例(或许是我使用的关键词不对),并成功试验,感谢相关贡献者。

答案一:

for /f "tokens=1-5 delims= " %%a in ('"netstat -ano|findstr "^:指定端口号""') do taskkill /pid %%e

 

答案二:

  1. @echo off
  2. set port=1234
  3. for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do taskkill /pid %%m

 

我自己试验成功的脚本如下:不加/f就关不掉

@echo off
REM 声明采用UTF-8编码
chcp 65001
set port=9999
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do (
    echo kill the process %%m who use the port %port%
    echo 正在关闭,请等待 %%m
    taskkill /f /pid %%m
)
pause

参考地址:http://zhidao.baidu.com/link?url=wGa-tP8nkQfi2bla1BRN5ZYjZ1TZzs3sr1QCPS0tS2NHbiUee9byFCNO31imk337DHvOanD05ezYL0NMF0Hopa

 

循环读取一批端口,并查询对应PID是否存在,若存在则关闭,脚本如下:

  1. @echo off
  2. for /l %%n in (20801,1,20812) do (
  3. @echo find the process which use port [%%n]
  4. for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%%n"') do (
  5. tasklist /FI "PID eq %%m"|find /i "PID" && (
  6. echo PID:%%m 运行中,kill the process [%%m] who use the port [%%n]
  7. taskkill /F /pid %%m
  8. ) || echo PID:%%m 未运行
  9. )
  10. )

以上脚本,参考资料如下:

http://blog.csdn.net/painss/article/details/4431580

http://3y.uu456.com/bp_1fudk0d4or4n7xz5ebbq_1.html

原文地址:https://blog.csdn.net/jacke121/article/details/70187977
原文地址:https://www.cnblogs.com/jpfss/p/11102431.html