通过从 .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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
打开命令提示符并运行以下命令
,这里3116是进程ID
Open command prompt and run the following commands
, here 3116 is the process ID
下面是一个帮助您入门的命令:
当您对批处理文件有信心时,请删除
@ECHO
。请注意,您可能需要针对不同的操作系统稍微更改此设置。例如,在 Windows 7 上,您可能需要
tokens=5
而不是tokens=4
。工作原理
这允许您执行
命令
,并循环其输出。每行都将被填充到%variable
中,并且可以在otherCommand
中展开任意多次,无论您喜欢在哪里。%variable
在实际使用中只能是单字母名称,例如%V
。这使您可以通过空格分割每一行,并获取该行中的第四个块,并将其填充到
%variable
中(在我们的例子中为%%P
)。delims
看起来是空的,但额外的空间实际上很重要。只需运行它即可找出答案。根据命令行帮助,它“显示所有连接和侦听端口。”,“以数字形式显示地址和端口号。”,以及“显示与每个连接关联的所属进程ID。”。我只是使用了这些选项,因为其他人建议了它,而且它碰巧起作用了:)
这获取第一个命令或程序 (
netstat
) 的输出并将其传递到第二个命令程序 (查找str
)。如果您直接在命令行上而不是在命令字符串中使用它,则可以使用|
而不是^|
。这会过滤传递给它的任何输出,仅返回包含
:8080
的行。这会使用进程 ID 终止正在运行的任务。
这在批处理文件中是必需的。如果您在命令提示符下执行此操作,则将使用
%P
代替。Here's a command to get you started:
When you're confident in your batch file, remove
@ECHO
.Note that you might need to change this slightly for different OS's. For example, on Windows 7 you might need
tokens=5
instead oftokens=4
.How this works
This lets you execute
command
, and loop over its output. Each line will be stuffed into%variable
, and can be expanded out inotherCommand
as many times as you like, wherever you like.%variable
in actual use can only have a single-letter name, e.g.%V
.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.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 :)
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^|
.This filters any output that is passed into it, returning only lines that contain
:8080
.This kills a running task, using the process ID.
This is required in batch files. If you did this on the command prompt, you would use
%P
instead.要在命令行上查找特定进程,请使用下面的命令,这里 8080 是进程用来
杀死进程的端口,使用下面的命令,这里 21424 是进程 ID
To find specific process on command line use below command here 8080 is port used by process
to kill process use below command here 21424 is process id
使用 Merlyn 的解决方案会导致其他应用程序(例如 Firefox)被杀死。
这些进程使用相同的端口,但不作为侦听器:
例如:
因此,可以通过将“LISTENING”添加到 findstr 来排除这些进程,如下所示:
Using Merlyn's solution caused other applications to be killed like firefox.
These processes were using the same port, but not as a listener:
eg:
Therefore, can excluded these by adding "LISTENING" to the findstr as follows:
要列出端口 8080 上运行的所有进程,请执行以下操作。
然后运行以下命令来终止进程
To list all the process running on port 8080 do the following.
Then to kill the process run the following command
谢谢大家,只是补充一点,除非 /F 强制开关也与 TaskKill 一起发送,否则某些进程不会关闭。
另外,使用 /T 开关时,进程的所有辅助线程都将被关闭。
对于服务,需要获取服务的名称并执行:
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.
For services it will be necessary to get the name of the service and execute:
创建一个包含以下内容的bat文件,它接受端口号的输入
最后单击“Enter”退出。
Created a bat file with the below contents, it accepts the input for port number
Finally click "Enter" to exit.
只是为了完成:
进程
我想杀死连接到特定端口的所有进程,但不是侦听端口 9001 的命令(在 cmd shell 中)的
是: findstr:
netstat:
它之所以有效,是因为 netstat 打印出源端口,然后打印出目标端口,然后打印出 ESTABLISHED
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:
findstr:
netstat:
It works because netstat prints out the source port then destination port and then ESTABLISHED
与 Merlyn 的响应类似,但这也处理这些情况:
您不寻找的号码。您想要搜索一个
准确端口号,这样您就不会杀死随机的、无辜的进程!
答案。
这里是:
Similar to Merlyn's response, but this one handles these cases as well:
number that you're not looking for. You want to search for an
exact port number so that you do not kill a random, innocent process!
answers.
Here it is:
如果您想终止正在侦听端口 8080 的进程,可以使用 PowerShell。只需将
Get-NetTCPConnection
cmdlet 与停止进程
。经过测试,应该可以在 Windows 10 或 Windows Server 2016 上与 PowerShell 5 配合使用。但是,我想它也应该可以在安装了 PowerShell 5 的旧 Windows 版本上使用。
这是一个例子:
If you want to kill the process that's listening on port 8080, you could use PowerShell. Just combine
Get-NetTCPConnection
cmdlet withStop-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.
Here is an example:
步骤:
转到 apache tomcat 服务器的
conf
文件夹。就我而言,它是apache-tomcat-7.0.61\conf
因为我正在使用 apache-tomcat-7.0.61打开
server.xml
并更改端口根据您的意愿将号码从 8080 转移到任何其他端口。例如:8081,8082,8087 等现在转到
bin
文件夹并运行shutdown.bat
现在通过 eclipse 重新启动服务器。
现在您的项目将不间断地运行。
Steps:
Go to
conf
folder of your apache tomcat server. In my case,itsapache-tomcat-7.0.61\conf
as I am using apache-tomcat-7.0.61Open
server.xml
and change the port number from 8080 to any other port as your wish. For example:8081,8082,8087 etcNow go to
bin
folder and runshutdown.bat
Now restart the server through eclipse.
Now your project will work without any interruption.
如果有人正在寻找 Powershell 脚本:
此函数基本上执行上述函数的操作,但它采用 Powershell 脚本格式,因此您可以将其添加到您的 Powershell 配置文件中。要查找您的个人资料位置,请转到 powershell 并输入
echo $profile
If anyone is looking for a Powershell Script:
This function basically does what the above functions do, but it is in the Powershell scripting format so you can add it to your Powershell profile. To find your profile's location go to powershell and type
echo $profile
请将其粘贴到命令行中pu
%%P
而不是%P
如果您想在批处理中使用它,
Paste this into command line
If you want to use it in a batch pu
%%P
instead of%P