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----------------------------------------------------
相关推荐
MobiCetus4 小时前
有关pip与conda的介绍
linux·windows·python·ubuntu·金融·conda·pip
丑过三八线8 小时前
【Kafka】Kafka4.0在windows上启动
windows·分布式·kafka
钢铁男儿13 小时前
Python 序列构成的数组(对序列使用+和_)
服务器·windows·python
DogDaoDao13 小时前
从零开始:Windows 系统中 PowerShell 配置 FFmpeg 的详细步骤
windows·ffmpeg·音视频·ffplay·powershell·视频直播·ffprobe
sukalot15 小时前
Windows 图形显示驱动开发-WDDM 2.4功能-GPU 半虚拟化(十二)
windows·驱动开发
安 当 加 密18 小时前
如何在Windows服务器上搭建RADIUS认证服务器,有哪些开源方案和付费解决方案
服务器·windows·开源
三体世界20 小时前
C++ List的模拟实现
java·c语言·开发语言·数据结构·c++·windows·list
落淼喵_G1 天前
【windows搭建lvgl模拟环境(一)之VSCode】
ide·windows·vscode
Tee xm1 天前
清晰易懂的Rust安装与配置教程
linux·windows·macos·rust
小比卡丘1 天前
【C++初阶】第12课—list
c++·windows·list