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----------------------------------------------------
相关推荐
遇到困难睡大觉哈哈8 分钟前
Harmony os LazyForEach:数据懒加载详解
服务器·网络·windows·harmonyos·鸿蒙
繁华似锦respect20 分钟前
C++ 无锁队列(Lock-Free Queue)详细介绍
linux·开发语言·c++·windows·visual studio
u***45161 小时前
Windows安装Rust环境(详细教程)
开发语言·windows·rust
A懿轩A1 小时前
【2025版 OpenHarmony】 GitCode 口袋工具:Flutter + Dio 网路请求 打造随身的鸿蒙版 GitCode 搜索助手
windows·flutter·华为·鸿蒙·openharmony·开源鸿蒙
f***24111 小时前
不常用,总是忘记:nginx 重启指令
运维·windows·nginx
de之梦-御风1 小时前
【远程控制】RustDesk 自建服务端完整方案(Docker + Windows 客户端)
windows·docker·容器
气概2 小时前
WINDOWS系统安装
windows
g***86692 小时前
Windows上安装Go并配置环境变量(图文步骤)
开发语言·windows·golang
小龙报2 小时前
VS2022调试技巧 + 实战案例
android·服务器·c语言·数据库·c++·windows·visual studio
2301_795167202 小时前
Python 高手编程系列一十三:现实例子 — 延迟求值属性
开发语言·windows·python