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----------------------------------------------------
相关推荐
老王头的笔记38 分钟前
Spring支持的消费器模式,支持在当前事务提交、或回滚的前、后执行业务操作
java·windows·spring
专注代码七年4 小时前
Spring Boot DevTools 热部署完整配置指南
windows
无惧代码5 小时前
FNM Windows 安装教程
windows
uxiang_blog5 小时前
如何使用U盘在Windows电脑上按装Fedora43 KDE?
linux·windows·重装系统·fedora43
互亿无线明明5 小时前
在 Go 项目中集成国际短信能力:从接口调试到生产环境的最佳实践
开发语言·windows·git·后端·golang·pycharm·eclipse
冯旭_Android6 小时前
Windows电脑 也可以丝滑的使用 md5 文件 获取md5值
windows
我送炭你添花6 小时前
OpenWrt 22.03.5 命令行配置详解(TP-LINK WDR4320)
服务器·网络·windows
lihui_cbdd6 小时前
Windows11排查显卡问题导致的系统卡顿
运维·windows
rockingdingo6 小时前
利用 OneKey MCP Router Python SDK构建多领域大模型Function Call多工具调用数据集
网络·windows·python·ai agent·mcp
永不复还7 小时前
Windows APC注入解析
windows·windows内核·远程注入