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----------------------------------------------------
相关推荐
我命由我123454 小时前
Windows 操作系统 - Windows 查看架构类型
运维·windows·笔记·学习·系统架构·运维开发·系统
Byron Loong7 小时前
【逆向】Windows 三大注入:远程线程 、APC 、 钩子注入
windows
凤舞飘伶7 小时前
windows安装docker-desk
windows·docker·容器
绿豆人8 小时前
Cache缓存项目学习4
windows·学习·缓存
LuDvei8 小时前
Windows x86 架构下 Ubuntu 虚拟机内打包 Qt 文件指南
windows·qt·ubuntu
数据法师11 小时前
Wise Force Deleter:专门解决Windows文件“无法删除”问题的免费工具
windows
学习中.........11 小时前
Windows 上用 cc-connect 接入 Telegram + Codex:从零跑通与避坑指南
windows
铁手飞鹰11 小时前
PyModbus API对应的功能码和报文
linux·windows·python·modbus rtu·pymodbus
idolao12 小时前
helium_0.11.5.1_x64-installer浏览器安装步骤详解(附Helium浏览器配置与隐私设置)
windows
爱宇阳12 小时前
Windows 11 右键菜单恢复经典样式完整指南
windows