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----------------------------------------------------
相关推荐
Eugene__Chen30 分钟前
内存管理详解(曼波脑图超详细版!)
java·jvm·windows
我不是立达刘宁宇2 小时前
subprocess 模块
windows
vortex54 小时前
深入浅出:LDAP 协议全面解析
网络·windows
三体世界8 小时前
Linux 动、静态库的实现
linux·开发语言·数据结构·c++·windows·算法
may_一一8 小时前
Redis增删改查
数据库·windows·redis
私人珍藏库9 小时前
[Windows] Adobe Camera Raw 17.2 win/Mac版本
windows·adobe
月临水9 小时前
Windows 11设置开机自动运行 .jar 文件
java·windows·jar
csdn_aspnet10 小时前
Windows Server .NET Core 应用程序部署到 IIS 解决首次访问加载慢的问题
windows·iis·.netcore
网络探索者12 小时前
奥创中心卸载工具Armoury Crate Uninstall Tool官网下载
windows
繁星无法超越13 小时前
详解Windows(一)——系统盘下目录及文件详解
windows