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----------------------------------------------------
相关推荐
love530love8 分钟前
OpenClaw搭配LM Studio VS Ollama:Windows CUDA实战深度对比与完全配置指南
人工智能·windows·vllm·ollama·llama.cpp·lm studio·openclaw
yuweiade2 小时前
Redis服务安装自启动(Windows版)
数据库·windows·redis
fjhcom2 小时前
OpenClaw 完整部署教程(Windows + Ollama)
windows·ollama·openclaw
喵叔哟3 小时前
6. 【Blazor全栈开发实战指南】--组件通信与共享状态
linux·网络·windows
AI-小柒3 小时前
开发者一站式数据解决方案:通过 DataEyes API 一键配置智能数据采集与分析工具
大数据·人工智能·windows·http·macos
CHQIUU3 小时前
外置硬盘格式选择指南:Windows与Mac双系统通用方案
windows·macos
非凡ghost5 小时前
支持1000+格式:Wondershare Recoverit万能数据恢复
网络·windows·学习·软件需求·teamviewer
永远睡不够的入5 小时前
C++list详解
c++·windows·list
longson.6 小时前
本地部署 MuseTalk 数字人(windows)
windows
机器小乙6 小时前
【开源】2 分钟在 Windows 上搭建 AI Agent 运行环境:MachineY Engine 使用指南
人工智能·windows·ai·开源·openclaw