windows 通过从 .BAT 中查找正在使用的端口来终止进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6204003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:25:26  来源:igfitidea点击:

Kill a Process by Looking up the Port being used by it from a .BAT

windowsbatch-fileprocesskill-process

提问by Mike Flynn

In Windows what can look for port 8080 and try to kill the process it is using through a .BAT file?

在 Windows 中,什么可以查找端口 8080 并尝试通过 .BAT 文件终止它正在使用的进程?

回答by Mohit Singh

Open command prompt and run the following commands

打开命令提示符并运行以下命令

 C:\Users\username>netstat -o -n -a | findstr 0.0:3000
   TCP    0.0.0.0:3000      0.0.0.0:0              LISTENING       3116

C:\Users\username>taskkill /F /PID 3116

, here 3116 is the process ID

,这里3116是进程ID

回答by Merlyn Morgan-Graham

Here's a command to get you started:

这是一个让你开始的命令:

FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO @ECHO TaskKill.exe /PID %%P

When you're confident in your batch file, remove @ECHO.

当您对批处理文件有信心时,请删除@ECHO.

FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO TaskKill.exe /PID %%P

Note that you might need to change this slightly for different OS's. For example, on Windows 7 you might need tokens=5instead of tokens=4.

请注意,您可能需要针对不同的操作系统稍微更改此设置。例如,在 Windows 7 上,您可能需要tokens=5而不是tokens=4.

How this works

这是如何工作的

FOR /F ... %variable IN ('command') DO otherCommand %variable...

This lets you execute command, and loop over its output. Each line will be stuffed into %variable, and can be expanded out in otherCommandas many times as you like, wherever you like. %variablein actual use can only have a single-letter name, e.g. %V.

这使您可以执行command,并循环其输出。每一行都将被塞进 中%variable,并且可以随心所欲地展开otherCommand多次,随心所欲。 %variable在实际使用中只能有一个单字母的名字,例如%V.

"tokens=4 delims= "

This lets you split up each line by whitespace, and take the 4th chunk in that line, and stuffs it into %variable(in our case, %%P). delimslooks empty, but that extra space is actually significant.

这让您可以用空格分割每一行,并取该行中的第 4 个块,并将其塞入%variable(在我们的例子中,%%P)。 delims看起来很空,但额外的空间实际上很重要。

netstat -a -n -o

Just run it and find out. According to the command line help, it "Displays all connections and listening ports.", "Displays addresses and port numbers in numerical form.", and "Displays the owning process ID associated with each connection.". I just used these options since someone else suggested it, and it happened to work :)

只需运行它并找出答案。根据命令行帮助,它“显示所有连接和侦听端口。”、“以数字形式显示地址和端口号。”和“显示与每个连接关联的拥有进程ID。”。我只是使用了这些选项,因为有人建议它,它碰巧起作用了:)

^|

This takes the output of the first command or program (netstat) and passes it onto a second command program (findstr). If you were using this directly on the command line, instead of inside a command string, you would use |instead of ^|.

这将获取第一个命令或程序 ( netstat)的输出并将其传递给第二个命令程序 ( findstr)。如果您直接在命令行上使用它,而不是在命令字符串中,您将使用|代替^|.

findstr :8080

This filters any output that is passed into it, returning only lines that contain :8080.

这将过滤传递给它的任何输出,仅返回包含:8080.

TaskKill.exe /PID <value>

This kills a running task, using the process ID.

这会使用进程 ID 终止正在运行的任务。

%%P instead of %P

This is required in batch files. If you did this on the command prompt, you would use %Pinstead.

这在批处理文件中是必需的。如果您在命令提示符下执行此操作,则应%P改用。

回答by Girdhar Singh Rathore

To find specific process on command line use below command here 8080 is port used by process

要在命令行上查找特定进程,请使用以下命令,此处 8080 是进程使用的端口

netstat -ano | findstr 8080

to kill process use below command here 21424 is process id

在这里使用下面的命令杀死进程 21424 是进程 ID

taskkill /pid 21424 /F 

回答by Solubris

Using Merlyn's solution caused other applications to be killed like firefox. These processes were using the same port, but not as a listener:

使用 Merlyn 的解决方案会导致其他应用程序像 Firefox 一样被杀死。这些进程使用相同的端口,但不是作为侦听器:

eg:

例如:

netstat -a -n -o | findstr :8085
  TCP    0.0.0.0:8085           0.0.0.0:0              LISTENING       6568
  TCP    127.0.0.1:49616        127.0.0.1:8085         TIME_WAIT       0
  TCP    127.0.0.1:49618        127.0.0.1:8085         TIME_WAIT       0

Therefore, can excluded these by adding "LISTENING" to the findstr as follows:

因此,可以通过将“LISTENING”添加到 findstr 来排除这些,如下所示:

FOR /F "tokens=5 delims= " %%P IN ('netstat -a -n -o ^| findstr :8085.*LISTENING') DO TaskKill.exe /PID %%P

回答by Sardesh Sharma

To list all the process running on port 8080 do the following.

要列出在端口 8080 上运行的所有进程,请执行以下操作。

netstat -ano | find "8080"

netstat -ano | 找到“8080”

TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       10612

TCP    [::]:8080              [::]:0                 LISTENING       10612

Then to kill the process run the following command

然后杀死进程运行以下命令

taskkill /F /PID 10612

taskkill /F /PID 10612

回答by xtrm

Thank you all, just to add that some process wont close unless the /F force switch is also send with TaskKill. Also with /T switch, all secondary threads of the process will be closed.

谢谢大家,只是补充一点,除非 /F 强制开关也与 TaskKill 一起发送,否则某些进程不会关闭。同样使用 /T 开关,进程的所有辅助线程都将关闭。

C:\>FOR /F "tokens=5 delims= " %P IN ('netstat -a -n -o ^|
 findstr :2002') DO TaskKill.exe /PID %P /T /F
C:\>FOR /F "tokens=5 delims= " %P IN ('netstat -a -n -o ^|
 findstr :2002') DO TaskKill.exe /PID %P /T /F

For services it will be necessary to get the name of the service and execute:

对于服务,需要获取服务的名称并执行:

sc stop ServiceName

sc 停止服务名称

回答by Boop

Just for completion:

只为完成:

I wanted to kill all processes connected to a specific port but not the process listening

我想杀死连接到特定端口的所有进程,但不杀死正在侦听的进程

the command (in cmd shell) for the port 9001 is:

端口 9001 的命令(在 cmd shell 中)是:

FOR /F "tokens=5 delims= " %P IN ('netstat -ano ^| findstr -rc:":9001[ ]*ESTA"') DO TaskKill /F /PID %P

findstr:

查找字符串

  • r is for expressions and c for exact chain to match.
  • [ ]* is for matching spaces
  • r 用于表达式,c 用于精确匹配链。
  • [ ]* 用于匹配空格

netstat:

网络统计

  • a -> all
  • n -> don't resolve (faster)
  • o -> pid
  • 一个 -> 全部
  • n -> 不解决(更快)
  • o -> pid

It works because netstat prints out the source port then destination port and then ESTABLISHED

它有效是因为 netstat 先打印出源端口,然后是目标端口,然后是 ESTABLISHED

回答by Sireesh Yarlagadda

Created a bat file with the below contents, it accepts the input for port number

创建了一个包含以下内容的 bat 文件,它接受端口号的输入

@ECHO ON
set /p portid=Enter the Port to be killed: 
echo %portid%                                                                              
FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr %portid% ') DO (
SET /A ProcessId=%%T) &GOTO SkipLine                                                   
:SkipLine                                                                              
echo ProcessId to kill = %ProcessId%
taskkill /f /pid %ProcessId%
PAUSE

Finally click "Enter" to exit.

最后点击“Enter”退出。

回答by bahrep

If you want to kill the process that's listening on port 8080, you could use PowerShell. Just combine Get-NetTCPConnectioncmdlet with Stop-Process.

如果要终止侦听端口 8080 的进程,可以使用 PowerShell。只需将Get-NetTCPConnectioncmdlet 与Stop-Process.

Tested and should work with PowerShell 5 on Windows 10 or Windows Server 2016. However, I guess that it should also work on older Windows versions that have PowerShell 5 installed.

经过测试,应该可以在 Windows 10 或 Windows Server 2016 上使用 PowerShell 5。但是,我想它也应该可以在安装了 PowerShell 5 的旧 Windows 版本上使用。

Here is an example:

下面是一个例子:

PS C:\> Stop-Process -Id (Get-NetTCPConnection -LocalPort 8080).OwningProcess

Confirm
Are you sure you want to perform the Stop-Process operation on the following item: MyTestServer(9408)?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

回答by Eduard Florinescu

Paste this into command line

将此粘贴到命令行中

FOR /F "tokens=5 delims= " %P IN ('netstat -ano ^| find "LISTENING" ^| find ":8080 "') DO (TASKKILL /PID %P)

If you want to use it in a batch pu %%Pinstead of %P

如果你想在批量 pu 中使用它%%P而不是%P