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----------------------------------------------------
相关推荐
eggwyw1 小时前
PHP搭建开发环境(Windows系统)
开发语言·windows·php
运维行者_3 小时前
通过OpManager的Windows服务监控能力释放最佳IT网络性能
服务器·开发语言·网络·windows·web安全·php
oscar9993 小时前
Windows下快速安装OpenCode及使用—PowerShell+Chocolatey
windows·opencode
元Y亨H4 小时前
Python 获取 Windows 设备信息笔记
windows·python
xiaoshuaishuai84 小时前
C# Submodule 避坑指南
服务器·数据库·windows·c#
一个小浪吴啊7 小时前
MacOS/Linux/Windows 跨平台一键安装OpenCode指南
linux·windows·macos·opencode
小草cys7 小时前
最简单:一根网线直连,mac mini Windows 控制 Mac mini mac
windows·macos·局域网直连
阿昭L7 小时前
调试Windows11启动过程
windows·uefi·windows内核
无巧不成书02188 小时前
Windows环境变量故障排查:记事本BOM头导致配置失效终极解决方案 | 零基础全流程指南
windows·批处理脚本故障·windows故障排查·windows记事本·bom头·utf-8 bom·环境变量读取失效 环境变量配置
FuckPatience8 小时前
Visual Studio的配置管理器
windows·visual studio