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----------------------------------------------------
相关推荐
Chukai1232 分钟前
Windows 和 Linux 系统下修改防火墙机制开放端口
linux·运维·windows
IT小农工2 小时前
Windows 文件资源管理器无法预览文件内容word、ppt、excel、pdf
windows·word·powerpoint
要记得喝水3 小时前
C#某公司面试题(含题目和解析)--1
开发语言·windows·面试·c#·.net
小韩博7 小时前
Windows 权限提升(一)
windows
魔都吴所谓9 小时前
【Python】根据开始时间、结束时间计算中间时间
开发语言·windows·python
syty202010 小时前
flink 伪代码
java·windows·flink
IDOlaoluo10 小时前
TortoiseGit 2.4.0.0 64位安装教程(附详细步骤和Git配置 附安装包)
windows
知行力11 小时前
【GitHub每日速递】不止 TeamViewer 替代!RustDesk 与 PowerToys,Windows 效率神器
windows·github·teamviewer
怒码ing12 小时前
List<?>和List<Object>区别
windows·python·list
豆浩宇14 小时前
学习PaddlePaddle--环境配置-Windows 11 + RTX 4060
人工智能·windows·深度学习·学习·目标检测·计算机视觉·paddlepaddle