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----------------------------------------------------
相关推荐
DogDaoDao5 小时前
【GitHub】System Informer:Windows 平台最强开源系统监控与调试利器
windows·程序员·开源·github·开发工具·system informer
HashFlag6 小时前
Codex配置Skill
windows·ai·mac·codex
idolao6 小时前
RayLink_v8.1.6.8安装步骤详解(附RayLink远程控制与设备码连接教程)
windows
lingx_gps8 小时前
Windows 下安装领新北斗(TracSeek)车辆动态监控系统小白指南
windows·jt808·车辆监控·主动安全·jt1078·车辆定位
2601_958320579 小时前
【详细版教程】Windows/macOS/Linux 安装 OpenClaw 2.6.6 指南(包含安装包)
linux·运维·windows·macos·小龙虾·open claw一键安装
HackTwoHub9 小时前
可视化未授权访问批量探测工具、支持批量目标、并发扫描、SOCKS5 全局代理、CSV 导出
linux·windows·macos·网络安全·网络攻击模型
50万马克的面包10 小时前
C 语言第18讲:预处理详解
c语言·开发语言·windows
无限进步_11 小时前
简单聊聊 C++ 中的 unordered_map 和 unordered_set
c语言·开发语言·数据结构·c++·windows·哈希算法·散列表
the sun3412 小时前
共享文件夹设置:让Windows和Ubuntu能方便的传输文件
linux·windows·ubuntu
拉拉拉拉拉拉拉马12 小时前
Windows 从 0 配置 Codex CLI 并接入外部 API:以小米 MiMo 为例
windows