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----------------------------------------------------
相关推荐
啥咕啦呛3 小时前
java打卡学习1:Lambda表达式与Stream基础
windows
优选资源分享5 小时前
zTasker v2.3.8 便携版丨 Windows 自动化任务工具
运维·windows·自动化
CQU_JIAKE7 小时前
3.23[Q]s
开发语言·windows·python
WJX_KOI8 小时前
OpenClaw 纯 Windows 环境源码部署教程(含修改脚本配置)
人工智能·windows
ulias2129 小时前
函数栈帧的创建和销毁
开发语言·数据结构·c++·windows·算法
攻城狮在此9 小时前
Windows电脑如何关闭不必要启动项,提升开机速度与运行流畅度
windows
ljh5746491199 小时前
linux xargs 命令
linux·运维·windows
开开心心就好10 小时前
电子教材下载工具,支持多链接批量下载
windows·随机森林·计算机视觉·pdf·计算机外设·逻辑回归·excel
Coding的叶子10 小时前
Windows 下 如何安全地安装 龙虾(OpenClaw)
windows·安全·ai·openclaw·龙虾·龙虾安装·龙虾实验室
babytiger11 小时前
Windows 11 下格式化 Linux 分区 TF 卡(DiskPart 完整教程)
linux·运维·windows