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----------------------------------------------------
相关推荐
ycjunhua1 小时前
windows 安装PostgreSQL 数据库
数据库·windows·postgresql
一个人旅程~2 小时前
Linux Mint(Ubuntu)如何在没有网卡驱动情况下利用手机上网安装旧电脑网卡驱动程序指导书
linux·windows·经验分享·电脑
格林威3 小时前
AI视觉检测:模型量化后漏检率上升怎么办?
人工智能·windows·深度学习·数码相机·计算机视觉·视觉检测·工业相机
无限进步_3 小时前
【C++】寻找字符串中第一个只出现一次的字符
开发语言·c++·ide·windows·git·github·visual studio
Ops菜鸟(Xu JieHao)4 小时前
Linux 内网远程桌面Xrdp ~保姆级教程
linux·运维·服务器·windows·远程桌面·远程·xrdp
永不复还4 小时前
Windows 驱动开发(四)—— IRP Pending
windows·驱动开发
海寻山6 小时前
Java 泛型 (Generic) 入门到精通:语法 + 原理 + 实战 + 避坑
java·windows·python
idolao6 小时前
PixPin 1.5安装与配置教程 Windows版:解压+管理员运行+自定义路径+开机自启设置指南
windows
无限进步_6 小时前
【C++】私有虚函数与多态:访问权限不影响动态绑定
开发语言·c++·ide·windows·git·算法·visual studio
公子小六7 小时前
基于.NET的Windows窗体编程之WinForms事件简介
windows·microsoft·c#·.net