如何终止当前在 Windows 本地主机上使用端口的进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39632667/
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-08 19:40:27  来源:igfitidea点击:

How do I kill the process currently using a port on localhost in Windows?

windowscmdlocalhostportcommand-prompt

提问by KavinduWije

How can I remove the current process/application which is already assigned to a port?

如何删除已分配给端口的当前进程/应用程序?

For example: localhost:8080

例如: localhost:8080

回答by KavinduWije

Step 1:

第1步:

Open up cmd.exe (note: you mayneed to run it as an administrator, but this isn't always necessary), then run the below command:

打开 cmd.exe(注意:您可能需要以管理员身份运行它,但这并不总是必要的),然后运行以下命令:

netstat -ano | findstr :PORT_NUMBER

netstat -ano | findstr :PORT_NUMBER

(Replace PORT_NUMBER with the port number you want, but keep the colon)

(将 PORT_NUMBER 替换为您想要的端口号,但保留冒号)

The area circled in red shows the PID (process identifier). Locate the PID of the process that's using the port you want.

红色圈出的区域显示了 PID(进程标识符)。找到正在使用所需端口的进程的 PID。

Step 2:

第2步:

Next, run the following command:

接下来,运行以下命令:

taskkill /PID PORT_NUMBER /F

taskkill /PID PORT_NUMBER /F

(No colon this time)

(这次没有冒号)

Lastly, you can check whether the operation succeeded or not by re-running the command in "Step 1". If it was successful you shouldn't see any more search results for that port number.

最后,您可以通过重新运行“步骤 1”中的命令来检查操作是否成功。如果成功,您将不会再看到该端口号的任何搜索结果。

回答by afsarkhan10182

Step 1 (same is in accepted answerwritten by KavinduWije):

第 1 步(与KavinduWije所写的公认答案相同):

netstat -ano | findstr :yourPortNumber
netstat -ano | findstr :yourPortNumber

Change in Step 2 to:

将步骤 2 更改为:

tskill typeyourPIDhere 

Note: taskkillis not working in some git bash terminal

注意taskkill在某些 git bash 终端中不起作用

回答by Duracell De Monaco

With Windows 10 default tools:

使用 Windows 10 默认工具:

  • Step one:
  • 步骤1:

Open Windows PowerShell as Administrator

以管理员身份打开 Windows PowerShell

  • Step two:
  • 第二步:

Find PID (ProcessID) for port 8080:

查找端口 8080 的 PID (ProcessID):

netstat -aon | findstr 8080

TCP 0.0.0.0:8080 0.0.0.0:0 LISTEN 77777

TCP 0.0.0.0:8080 0.0.0.0:0 听 77777

  • Step three:
  • 第三步:

Kill the zombie process:

杀死僵尸进程:

taskkill /f /pid 77777

where "77777" is your PID

其中“77777”是您的PID

回答by Ara Yaghsizian

If you are using GitBash

如果你使用 GitBash

Step one:

步骤1:

netstat -ano | findstr :8080

Step two:

第二步:

taskkill /PID typeyourPIDhere /F 

(/Fforcefully terminates the process)

/F强行终止进程)

回答by todorm

In Windows PowerShellversion 1 or later to stop a process on port 3000 type:

Windows PowerShell版本 1 或更高版本中,要停止端口 3000 上的进程,请键入:

Stop-Process (,(netstat -ano | findstr :3000).split() | foreach {$[$.length-1]}) -Force

停止进程 (,(netstat -ano | findstr :3000).split() | foreach {$ [$.length-1]}) -Force



As suggested by @morganpdx here`s a more PowerShell-ish, better version:

正如@morganpdx 所建议的,这里有更多 PowerShell 风格、更好的版本:

Stop-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess -Force

停止进程 -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess -Force

回答by Mahesh Narwade

For use in command line:

在命令行中使用:

for /f "tokens=5" %a in ('netstat -aon ^| find ":8080" ^| find "LISTENING"') do taskkill /f /pid %a

For use in bat-file:

在 bat 文件中使用:

for /f "tokens=5" %%a in ('netstat -aon ^| find ":8080" ^| find "LISTENING"') do taskkill /f /pid %%a

回答by Fellow Stranger

If you already know the port number, it will probably suffice to send a software termination signal to the process (SIGTERM):

如果您已经知道端口号,则向进程发送软件终止信号 (SIGTERM) 可能就足够了:

kill $(lsof -t -i :PORT_NUMBER)

回答by driven_spider

I was running zookeeper on Windows and wasn't able to stop ZooKeeperrunning at 2181 port using zookeeper-stop.sh, so tried this double slash "//" method to taskkill. It worked

我在 Windows 上运行 zookeeper 并且无法使用 zookeeper-stop.sh停止在 2181 端口运行的ZooKeeper,因此尝试使用这种双斜杠“//”方法来执行任务。有效

     1. netstat -ano | findstr :2181
       TCP    0.0.0.0:2181           0.0.0.0:0              LISTENING       8876
       TCP    [::]:2181              [::]:0                 LISTENING       8876

     2.taskkill //PID 8876 //F
       SUCCESS: The process with PID 8876 has been terminated.

回答by Zuhair Taha

For Windows users, you can use the CurrPortstool to kill ports under usage easily:

对于 Windows 用户,您可以使用CurrPorts工具轻松杀死正在使用的端口:

Enter image description here

在此处输入图片说明

回答by flopcoder

You can do by run a bat file:

您可以通过运行 bat 文件来完成:

@ECHO OFF                                                                              
FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr "9797" ') DO (
SET /A ProcessId=%%T) &GOTO SkipLine                                                   
:SkipLine                                                                              
echo ProcessId to kill = %ProcessId%
taskkill /f /pid %ProcessId%
PAUSE