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----------------------------------------------------
相关推荐
belldeep6 分钟前
python:介绍 UV 安装,如何使用 UV 安装配置 OpenHarness
windows·python·环境变量·uv
雨浓YN12 小时前
OPC UA 通讯开发笔记 - 基于本地dll文件
windows·笔记
大强同学13 小时前
Obsidian CLI + Claude Code = 王炸组合
人工智能·windows·ai编程·cli
iceslime14 小时前
Windows10系统静音修复相关
windows·音频·修复
humors22114 小时前
AI工具合集,不定期更新
人工智能·windows·ai·工具·powershell·deepseek
Echo-J14 小时前
在 Windows 7 虚拟机上安装 VMware Tools 时遇到驱动无法安装的问题
windows
sR916Mecz20 小时前
pache Hop实战:Windows平台MySQL数据迁移的深度排错与性能调优
数据库·windows·mysql
小白电脑技术21 小时前
Windows家庭版开启原生远程桌面
windows
扑火的小飞蛾21 小时前
Windows 10 与 Kali Linux SSH 免密互信配置指南
linux·windows·ssh
Chris _data1 天前
从宿主机(Windows)通过 NAT 模式连接虚拟机(Ubuntu)中的 MySQL
windows·mysql·ubuntu