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----------------------------------------------------
相关推荐
渡我白衣1 小时前
并行的野心与现实——彻底拆解 C++ 标准并行算法(<execution>)的模型、陷阱与性能真相
java·开发语言·网络·c++·人工智能·windows·vscode
Jelly-小丑鱼1 小时前
windows的WFP过滤域名
windows·域名白名单·wfp
这儿有一堆花1 小时前
视频文件的技术逻辑解析
windows·macos
百事牛科技2 小时前
压缩包安全升级:分享WinRAR的两种加密功能
windows·winrar
欧恩意2 小时前
【Viusal Studio】关于增量链接机制
汇编·windows·bug
定_格3 小时前
windows本地启动项目并使用arthas调试
windows
ReaF_star3 小时前
【安防】Windows Server 2008虚拟机忘记密码的一时兴起
网络·windows·安全
山峰哥3 小时前
EcMenu:解锁 Windows 右键菜单的终极自由
windows·性能优化·软件工程·鼠标右键菜单工具
天魔老师4 小时前
WSL2 “system.vhd找不到” 错误修复教程(实测有效)
windows·wsl
要站在顶端4 小时前
Jenkins设备监控(手机、手表)适配Windows、Linux
windows·智能手机·jenkins