杀通过查找端口一个进程正在使用它从一个.BAT [英] Kill a Process by Looking up the Port being used by it from a .BAT

查看:1306
本文介绍了杀通过查找端口一个进程正在使用它从一个.BAT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows可以寻找什么端口8080并试图杀死它是通过一个.bat文件使用过程?

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

推荐答案

下面是一个命令让你开始:

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

当你在你的批处理文件自信,删除 @ECHO

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

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

请注意,你可能需要稍微改变这种对不同的操作系统。例如,在Windows 7中,您可能需要标记= 5 而不是标记= 4

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

这是如何工作

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

这使您可以执行命令,并遍历它的输出。每一行都将被装进%变量,并且可以在 otherCommand 扩大了许多次,只要你喜欢,只要你喜欢。 %变量在实际使用中只能有一个字母的名称,例如%V

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

"tokens=4 delims= "

这可以让你分裂由空格每一行,走在该行的第4块,并将其填充到%变量(在本例中, %% P )。 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). delims looks empty, but that extra space is actually significant.

netstat -a -n -o

只要运行它,并找出。根据命令行帮助下,显示所有连接和侦听端口,显示地址和端口号以数字形式。和显示与每个连接关联的所属进程ID。我只是用这些选项,因为别人提出的建议,它发生的工作:)

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 :)

^|

这需要的第一个命令或程序的输出(的netstat ),并把它拖到第二个命令程序( FINDSTR )。如果你直接使用此命令行上,而不是一个命令字符串中,你可以使用 | 而不是 ^ |

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 ^|.

findstr :8080

这过滤传递到它的任何输出,只返回包含行:8080

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

TaskKill.exe /PID <value>

这杀死一个正在运行的任务,使用过程中ID。

This kills a running task, using the process ID.

%%P instead of %P

这是必需的批处理文件。如果你这样做的命令提示符下,你可以使用%P 代替。

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

这篇关于杀通过查找端口一个进程正在使用它从一个.BAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆