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----------------------------------------------------
相关推荐
洛水如云28 分钟前
换新电脑数据迁移全攻略:2 种方法转移文件 / 系统
windows·microsoft·电脑
chushiyunen1 小时前
posting替换postman(好像还是不太好用)
windows
overmind1 小时前
oeasy Python 120[专业选修]列表_直接赋值_浅拷贝_shallowcopy_深拷贝_deepcopy
linux·windows·python
ulias2122 小时前
Linux中的开发工具
linux·运维·服务器·开发语言·c++·windows
Ai.den2 小时前
Windows 安装 DeerFlow 2.0
人工智能·windows·python·ai
openallzzz2 小时前
【多动手】windows 下使用 docker 启动 Java 服务(docker desktop + wsl)
java·运维·windows·docker·容器
小熊Coding2 小时前
Windows 上安装 mysqlclient 时遇到了编译错误,核心原因是缺少 Microsoft Visual C++ 14.0 或更高版本 的编译环境。
c++·windows·python·microsoft·django·mysqlclient·bug记录
abolbee4 小时前
Cursor 接入 Figma MCP 实战经验贴Windows版
windows·figma·cursor
xiaoshuaishuai84 小时前
C# 实现Workstation相关功能
开发语言·windows·c#
水饺编程4 小时前
第5章,[标签 Win32] :GDI 的基本图形
c语言·c++·windows·visual studio