Windows下使用Bat文件杀死进程

输入端口号杀死进程

新建一个"kill port.bat"文件

复制代码
@echo off
rem open var delay
setlocal enabledelayedexpansion
rem recev var 
set /p port=请输入端口号
if "%port%" == "" (
    echo Please enter port
    goto end
)
echo ---------------start find who is using port %port%----------------------- 
rem Add colon before port and space after port to ensure no mistake
for /f "tokens=1-5" %%a in ('netstat -ano ^| find ":%port% "') do (
    rem a is protocol,b is local ip:port, c is remote ip:port,d is state or pid,e is pid or null 
    echo %%a %%b %%c %%d %%e
    rem Cut the %%b with the : to get the port
    for /f "delims=:, tokens=1-2" %%j in ("%%b") do (set destport=%%k)
    rem Delay variable in for loop must use! Symbol instead of% symbol
    echo The port found here is !destport!. What we need is %port%  
    rem If %%b cuts the same port as the target port
    if "!destport!" equ "%port%" (
        if "%%e" == "" (
            rem sometimes e is empty pid in d
            if not "%%d" == "0" (
                echo get d col pid %%d
                set pid=%%d
            )
        ) else (
            if not "%%e" == "0" (
               echo get e col pid %%e
               set pid=%%e
            )
        )
    ) else (
        echo this row local not use port %port% 
    )
)
echo ---------------end find who is using port %port%----------------------- 
if "%pid%"=="" (
 echo Port %port% is not in use 
) else (
 echo used port %port% the process %pid%
 taskkill /f /pid %pid%
)
pause

:end
echo ---------------end----------------------------------------------------
相关推荐
a***131416 小时前
redis存取list集合
windows·redis·list
lpfasd12320 小时前
Windows下使用nvm-windows管理Node.js版本及npm配置全指南
windows·npm·node.js
e***753921 小时前
在 Windows 上生成本地 SSL 证书并使用 HTTPS 访问本地 Nginx 服务器
windows·https·ssl
laocooon5238578861 天前
实现了一个新闻数据采集与分析系统python
linux·服务器·windows
_oP_i1 天前
Win11 性能调优实用指南
windows
保持低旋律节奏1 天前
C++——C++11特性
开发语言·c++·windows
ol木子李lo1 天前
Visual studio 2022高亮汇编(ASM)语法方法
汇编·ide·windows·visual studio
王柏龙1 天前
Win11 无法找到本地组策略编辑器的解决方法
windows·策略模式
q***09801 天前
Windows 11 如何配置node.js
windows·node.js
q***47181 天前
Windows 上彻底卸载 Node.js
windows·node.js