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----------------------------------------------------
相关推荐
嘴贱欠吻!10 小时前
Kuikly搭建OpenHarmony教程01:源码构建与运行(Windows)
windows
2601_9494800612 小时前
Flutter for OpenHarmony 音乐播放器App实战 - 主题设置实现
windows·flutter
石像鬼₧魂石13 小时前
内网渗透学习框架:五维金字塔
windows·学习
体面:13 小时前
2026闲鱼监控助手
windows·经验分享
小雨青年16 小时前
环境准备 Windows Mac 下 Docker Desktop 的安装与镜像源加速
windows·macos·docker
数据知道17 小时前
一文掌握 MongoDB 详细安装与配置(Windows / Linux / macOS 全平台)
linux·数据库·windows·mongodb·macos
440资源库17 小时前
从 Windows 转向 macOS:数据迁移完整指南
windows·macos·win向maos迁移数据
xmRao17 小时前
Qt+FFmpeg 实现 Windows 音频采集
windows·qt·ffmpeg
njsgcs18 小时前
ModelScope下载模型+ vLLM调用+内存释放
windows·wsl·vllm
黄美美分享18 小时前
Windows自动化设置工具,无需编程让电脑自动工作!定时任务软件!打工人必备软件
运维·windows·自动化·电脑