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----------------------------------------------------
相关推荐
modest —YBW4 小时前
Ollama+OpenWebUI+docker完整版部署,附带软件下载链接,配置+中文汉化+docker源,适合内网部署,可以局域网使用
人工智能·windows·docker·语言模型·llama
code在飞4 小时前
windows 部署 Kafka3.x KRaft 模式 不依赖 ZooKeeper
windows·分布式·zookeeper·kafka
不会飞的鲨鱼6 小时前
Windows系统下使用Kafka和Zookeeper,Python运行kafka(二)
windows·zookeeper·kafka
2501_9153738815 小时前
Electron 打包与发布指南:让你的应用运行在 Windows、macOS、Linux
windows·macos·electron
田辛 | 田豆芽1 天前
【ASP.net】在Windows 11上安装IIS并测试C# Web项目的踩坑实录
windows·c#·asp.net
6v6-博客1 天前
闲鱼智能客服机器人-实现闲鱼平台7×24小时自动化值守
windows
哞哞不熬夜1 天前
JavaEE--文件操作和IO
java·开发语言·windows·学习·java-ee·intellij-idea·idea
奋斗鱼1 天前
如何清除windows 远程桌面连接的IP记录
windows
对 酒 当 歌 人 生 几 何1 天前
sui在windows虚拟化子系统Ubuntu和纯windows下的安装和使用
linux·windows·ubuntu
qq_393828221 天前
Windows ABBYY FineReader 16 Corporate 文档转换、PDF编辑和文档比较
windows·microsoft·电脑·开源软件·软件需求