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----------------------------------------------------
相关推荐
为何创造硅基生物3 小时前
嵌入式 LVGL / SquareLine UI 标准命名规则(行业通用版)
windows·ui
十八旬4 小时前
快速安装ClaudeCode完整指南
开发语言·windows·python·claude
谁的小耳朵6 小时前
一次小米电脑管家安装失败排查:官网安装包闪退,最后发现是埋点接口异常
windows
x***r1518 小时前
DLL错误专修工具_TBI3264安装步骤详解(附DLL缺失一键修复与运行库安装教程)
windows
阿萨德528号11 小时前
Windows RabbitMQ 启动完整指南(附启动报错解决、如何以服务方式后台运行)
windows·rabbitmq·ruby
私人珍藏库12 小时前
[吾爱大神原创工具] 鼠标轨迹美化工具
windows·工具·鼠标·软件·win·多功能
小小测试开发12 小时前
AI Agent 重构单体应用实战:1Password 经验与避坑指南
人工智能·windows·重构
您^_^12 小时前
专家(一):Claude Code 微服务实战——6 个服务从拆分到 K8s 部署,$0.45 全套 YAML 照抄
人工智能·windows·微服务·架构·kubernetes·个人开发·claude code
TE-茶叶蛋12 小时前
Java 8 引入的Stream API-stream()
java·windows·python
lei_68612 小时前
Microsoft Office Click-to-Run Service关闭服务
windows·microsoft