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----------------------------------------------------
相关推荐
love530love13 小时前
MingLi-Bench 项目部署实录:基于 EPGF 架构的工程化实践
人工智能·windows·python·架构·aigc·epgf·mingli-bench
leazer16 小时前
Flutter Windows 构建失败:.plugin_symlinks 符号链接异常的排查与修复
windows·flutter
大貔貅喝啤酒17 小时前
基于Windows下载安装Android Studio 3.3.2版本教程(2026详细图文版)
android·java·windows·android studio
音视频牛哥18 小时前
大牛直播SDK(SmartMediaKit)Windows平台RTSP/RTMP直播播放SDK集成说明(C++版)
windows·音视频·实时音视频·windows rtsp播放器·windows rtmp播放器·超低延迟rtsp播放器·超低延迟rtmp播放器
Irene199118 小时前
Windows 11 WSL Ubuntu 环境:实际安装 Hive 踩坑实录
hive·windows·ubuntu
console.log('npc')19 小时前
Windows 11 → WSL2 → Ubuntu → Docker → Codex → Sub2API
windows·ubuntu·docker
ID_1800790547319 小时前
企业级实战:淘宝铺货核心API接口说明(含JSON返回)
windows
遇印记21 小时前
软考知识点(windows系统管理与命令)
运维·服务器·网络·windows·ddos
段ヤシ.21 小时前
Windows环境下安装Tomcat,并配置环境变量
windows·tomcat
IT WorryFree21 小时前
Windows 10/11(64位)上安装 WinQSB——无需虚拟机
windows