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

发布于 2024-11-10 18:45:06 字数 57 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

七禾 2024-11-17 18:45:06

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

 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

,这里3116是进程ID

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

冷情 2024-11-17 18:45:06

下面是一个帮助您入门的命令:

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

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

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

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

工作原理

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

这允许您执行命令,并循环其输出。每行都将被填充到 %variable 中,并且可以在 otherCommand 中展开任意多次,无论您喜欢在哪里。 %variable在实际使用中只能是单字母名称,例如%V

"tokens=4 delims= "

这使您可以通过空格分割每一行,并获取该行中的第四个块,并将其填充到 %variable 中(在我们的例子中为 %%P)。 delims 看起来是空的,但额外的空间实际上很重要。

netstat -a -n -o

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

^|

这获取第一个命令或程序 (netstat) 的输出并将其传递到第二个命令程序 (查找str)。如果您直接在命令行上而不是在命令字符串中使用它,则可以使用 | 而不是 ^|

findstr :8080

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

TaskKill.exe /PID <value>

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

%%P instead of %P

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

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.

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=5 instead of 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 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= "

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

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

findstr :8080

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

TaskKill.exe /PID <value>

This kills a running task, using the process ID.

%%P instead of %P

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

作妖 2024-11-17 18:45:06

要在命令行上查找特定进程,请使用下面的命令,这里 8080 是进程用来

netstat -ano | findstr 8080

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

taskkill /pid 21424 /F 

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

netstat -ano | findstr 8080

to kill process use below command here 21424 is process id

taskkill /pid 21424 /F 
拔了角的鹿 2024-11-17 18:45:06

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

例如:

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

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

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

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:

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:

FOR /F "tokens=5 delims= " %%P IN ('netstat -a -n -o ^| findstr :8085.*LISTENING') DO TaskKill.exe /PID %%P
楠木可依 2024-11-17 18:45:06

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

netstat -ano |找到“8080”

TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       10612

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

然后运行以下命令来终止进程

taskkill /F /PID 10612

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

netstat -ano | find "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

梦过后 2024-11-17 18:45:06

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

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

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

sc stop 服务名称

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.

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

漫雪独思 2024-11-17 18:45:06

创建一个包含以下内容的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

最后单击“Enter”退出。

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

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

夜空下最亮的亮点 2024-11-17 18:45:06

只是为了完成:

进程

我想杀死连接到特定端口的所有进程,但不是侦听端口 9001 的命令(在 cmd shell 中)的

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

是: findstr

  • r 表示表达式,c 表示精确链来匹配。
  • [ ]* 用于匹配空格

netstat

  • a ->所有
  • n ->不解决(更快)
  • o -> pid

它之所以有效,是因为 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:

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

netstat:

  • a -> all
  • n -> don't resolve (faster)
  • o -> pid

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

情绪操控生活 2024-11-17 18:45:06

与 Merlyn 的响应类似,但这也处理这些情况:

  • 端口号实际上是另一个较长端口的左子字符串
    您不寻找的号码。您想要搜索一个
    准确端口号,这样您就不会杀死随机的、无辜的进程!
  • 脚本代码需要能够运行多次并且每次都是正确的,而不是显示较旧的、不正确的
    答案。

这里是:

set serverPid=
for /F "tokens=5 delims= " %%P in ('netstat -a -n -o ^| findstr /E :8080 ') do set serverPid=%%P
if not "%serverPid%" == "" (
  taskkill /PID %serverPid%
) else (
  rem echo Server is not running.
)

Similar to Merlyn's response, but this one handles these cases as well:

  • The port number is actually a left substring of another longer port
    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!
  • The script code needs to be able to run more than once and be correct each time, not showing older, incorrect
    answers.

Here it is:

set serverPid=
for /F "tokens=5 delims= " %%P in ('netstat -a -n -o ^| findstr /E :8080 ') do set serverPid=%%P
if not "%serverPid%" == "" (
  taskkill /PID %serverPid%
) else (
  rem echo Server is not running.
)
淡淡的优雅 2024-11-17 18:45:06

如果您想终止正在侦听端口 8080 的进程,可以使用 PowerShell。只需将 Get-NetTCPConnection cmdlet 与停止进程

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

这是一个例子:

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

If you want to kill the process that's listening on port 8080, you could use PowerShell. Just combine Get-NetTCPConnection cmdlet with 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.

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"):
澉约 2024-11-17 18:45:06

步骤:

  1. 转到 apache tomcat 服务器的 conf 文件夹。就我而言,它是 apache-tomcat-7.0.61\conf 因为我正在使用 apache-tomcat-7.0.61

  2. 打开 server.xml 并更改端口根据您的意愿将号码从 8080 转移到任何其他端口。例如:8081,8082,8087 等

  3. 现在转到 bin 文件夹并运行 shutdown.bat

  4. 现在通过 eclipse 重新启动服务器。

现在您的项目将不间断地运行。

Steps:

  1. Go to conf folder of your apache tomcat server. In my case,its apache-tomcat-7.0.61\conf as I am using apache-tomcat-7.0.61

  2. Open server.xml and change the port number from 8080 to any other port as your wish. For example:8081,8082,8087 etc

  3. Now go to bin folder and run shutdown.bat

  4. Now restart the server through eclipse.

Now your project will work without any interruption.

染墨丶若流云 2024-11-17 18:45:06

如果有人正在寻找 Powershell 脚本:

function Search-And-Destroy
{
    param ( [Parameter(Mandatory=$true)][string]$port )
    $lines = netstat -a -o -n | findstr $port
    $ports = @()

    ForEach($line In $lines)
    {
        $res = $($lines -split '\s+')
        $ports += $res[5]
    }

    $ports = $ports | select -uniq

    ForEach($port In $ports)
    {
        echo $(taskkill /F /PID $port)
    }
}

此函数基本上执行上述函数的操作,但它采用 Powershell 脚本格式,因此您可以将其添加到您的 Powershell 配置文件中。要查找您的个人资料位置,请转到 powershell 并输入 echo $profile

If anyone is looking for a Powershell Script:

function Search-And-Destroy
{
    param ( [Parameter(Mandatory=$true)][string]$port )
    $lines = netstat -a -o -n | findstr $port
    $ports = @()

    ForEach($line In $lines)
    {
        $res = $($lines -split '\s+')
        $ports += $res[5]
    }

    $ports = $ports | select -uniq

    ForEach($port In $ports)
    {
        echo $(taskkill /F /PID $port)
    }
}

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

街角卖回忆 2024-11-17 18:45:06

请将其粘贴到命令行中pu %%P 而不是 %P

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

如果您想在批处理中使用它,

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 %%P instead of %P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文