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----------------------------------------------------
相关推荐
一瓢西湖水8 小时前
Windows安装OpenClaw实践指南
人工智能·windows·ai
sjmaysee9 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
酿情师9 小时前
Windows Subsystem for Linux (WSL, Ubuntu)安装教程(详细)
linux·windows·ubuntu
love530love11 小时前
ComfyUI rgthree-comfy Image Comparer 节点无输出问题排查与解决
人工智能·windows·python·comfyui·rgthree-comfy·nodes 2.0·vue 节点
cga194714 小时前
【RabbitMQ】超详细Windows系统下RabbitMQ的安装配置
windows·分布式·rabbitmq
jason_renyu16 小时前
Windows下MySQL多实例配置:添加多端口服务(独立配置文件法)
windows·mysql·mysql配置多端口服务
VeryDelicious17 小时前
Cursor怎么手动安装插件,怎么复制别人的插件
windows·cursor·日常开发笔记
漫雾_17 小时前
被 Lazarus 长期利用的漏洞:Windows AppLocker 内核模式权限提升漏洞复现
c++·windows·安全
非凡ghost18 小时前
1by1(轻量级音乐播放器)
windows·学习·音视频·软件需求·teamviewer
咖啡の猫19 小时前
Redis命令-List命令
windows·redis·list