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----------------------------------------------------
相关推荐
2501_941982055 小时前
企业微信二次开发:私域数据基石——三大核心基础能力接口实战
windows·架构·bootstrap·企业微信
美丽的欣情14 小时前
RK3588 Debian 交叉编译环境搭建(Windows + VMware Debian + CMake)
运维·windows·debian
勤劳打代码15 小时前
Flutter 架构指南 —— windows 平台安装拆解
windows·架构
aramae19 小时前
C++11:现代C++的里程碑
c语言·开发语言·c++·windows·git·后端
执行x19 小时前
wsl2安装+桥接模式+固定IP
linux·windows·ubuntu
做萤石二次开发的哈哈20 小时前
如何接入萤石开放平台 ERTC Windows SDK?
windows·音视频·萤石开放平台
橙臣程20 小时前
在 Windows 中使用 WSL2 搭建 Ubuntu 开发环境
linux·windows·ubuntu
霸道流氓气质21 小时前
Windows 本地搭建 CDC 全链路测试环境(Kafka+Canel+binlog+SpringBoot)
windows·spring boot·kafka
郝开21 小时前
Windows 安装 Ostris AI Toolkit
人工智能·windows·toolkit·ai toolkit
疯狂成瘾者2 天前
Java 常见集合方法
java·windows·python