killport bat脚本 强制杀死占用端口的进程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了killport bat脚本 强制杀死占用端口的进程相关的知识,希望对你有一定的参考价值。

参考技术A https://blog.csdn.net/PiaoMiaoXiaodao/article/details/89792789

@echo off
rem open var delay
setlocal enabledelayedexpansion
rem recev var
set port=%1%
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%
)

:end
echo ---------------end----------------------------------------------------

windows查看进程占用并强制结束进程

打开命令提示符(CMD)

查看8080端口被哪个进程占用了,命令:netstat   -ano|findstr 8080

技术分享图片

上面的8080端口的PID是30160,可以根据PID可以杀死这个进程,用下面的命令:
tasklist|findstr 30160;
taskkill /pid 30160 -t -f;

 

以上是关于killport bat脚本 强制杀死占用端口的进程的主要内容,如果未能解决你的问题,请参考以下文章

windows杀死某个占用端口的进程

mac os如何查看端口占用情况以及杀死进程?

mac os如何查看端口占用情况以及杀死进程?

mac os如何查看端口占用情况以及杀死进程?

linux 怎么用 kill 命令 结束占用 80端口的 进程

shell常用命令