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----------------------------------------------------
相关推荐
NGBQ121381 小时前
K-Lite_Codec_Pack_1957_Mega.exe 全解析:Windows 端全能音视频解码器深度指南
windows·音视频
阿杜杜不是阿木木1 小时前
从0到1构建像Claude Code那样的Agent(三):行动前先计划
java·服务器·windows·agent·ai编程·claudecode
张道宁10 小时前
Windows 环境下 Docker 部署 YOLOv8 并集成 Spring Boot 完整指南
windows·yolo·docker
万里沧海寄云帆13 小时前
Windows下uv环境硬链接失败问题
windows·uv
unsynchronized14 小时前
OpenClaw 安装指南:Linux 与 Windows 系统详细教程
linux·运维·windows·ai
x***r15115 小时前
R语言 4.5.1安装教程 Windows版:解压+管理员运行+自定义路径+启动验证指南
windows
非凡ghost17 小时前
Zen Browser:基于 Firefox 的极简开源浏览器,隐私与速度兼得
前端·网络·windows·学习·开源·firefox·软件需求
木梓辛铭17 小时前
windows下部署openclaw详细方法及错误处理办法
windows·学习方法·持续部署
John_ToDebug17 小时前
死锁案例:UI 线程阻塞等待跨进程 COM 注入
c++·windows·笔记
oscar99920 小时前
Memurai:Redis官方认可的Windows原生解决方案
数据库·windows·redis