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----------------------------------------------------
相关推荐
柳鲲鹏5 小时前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
专注VB编程开发20年8 小时前
开机自动后台运行,在Windows服务中托管ASP.NET Core
windows·后端·asp.net
李洋-蛟龙腾飞公司10 小时前
HarmonyOS NEXT应用元服务常见列表操作分组吸顶场景
linux·运维·windows
码农垦荒笔记11 小时前
Git 安装闭坑指南(仅 Windows 环境)
windows·git
阿幸软件杂货间12 小时前
Windows 10 2016 长期服务版
windows·系统·win10
木头左15 小时前
Windows环境下Docker容器化的安装与设置指南
windows·docker·容器
qq_3938282215 小时前
PDF的图片文字识别工具
windows·pdf·电脑·软件需求·图片处理
tonydf16 小时前
记一次近6万多个文件的备份过程
windows·后端
lsec17 小时前
内存加载带有TLS的DLL的问题分析
windows·安全
spencer_tseng17 小时前
Windows DOS CMD 100
windows·dos·cmd