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----------------------------------------------------
相关推荐
oh LAN几秒前
Windows 下 Redis 开机自启
数据库·windows·redis
Warren9826 分钟前
Windows本地部署n8n完整教程(基于Docker,新手友好)
运维·windows·python·测试工具·docker·容器·可用性测试
学安全的小白2 小时前
VMware安装windows server 2003 R2 教程(图文版)
服务器·windows
不过如此19513 小时前
pyinstaller打包GUI项目实践
windows·python·ui
Vanranrr3 小时前
从图形化到命令行:一次 SVN 工程化能力补齐与流程治理实践(Windows)
windows·svn
神奇椰子4 小时前
Windows远程桌面连接(mstsc)丢失/无法打开的修复指南
windows
高斯的手稿08014 小时前
树莓派上更换镜像源的方法
linux·运维·windows
SunkingYang4 小时前
怎么将VSCode添加到右键菜单
windows·vscode·注册表·删除·添加·右键菜单
我妻礼弥5 小时前
windows 11 配置 UTF-8
windows
唐朝板栗丶TDC5 小时前
Windows下使用WSL2创建Ubuntu子系统(更改安装位置与启动图形桌面)
linux·windows·经验分享·ubuntu