这篇文章发布于我的 github 博客:原文
今天在本地调试 Blog 的时候意外的出现了一些错误:127.0.0.1 4000 端口已经被其他的进程占用了。如何找到占用端口的进程呢?
Configuration file: /_config.yml
Source: .
Destination: ./_site
Generating...
done.
Please add the following to your Gemfile to avoid polling for changes:
gem 'wdm', '>= 0.1.0' if Gem.win_platform?
Auto-regeneration: enabled for '.'
Configuration file: /_config.yml
jekyll 2.4.0 | Error: Permission denied - bind(2) for 127.0.0.1:4000
我们可以使用 netstat
命令查看各种端口的被进程的占用情况,例如,开一个命令行或者 powershell 窗口输入 netstat -ano
。就得到了如下的报告。
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 888
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:1536 0.0.0.0:0 LISTENING 564
TCP 0.0.0.0:1537 0.0.0.0:0 LISTENING 848
TCP 0.0.0.0:1538 0.0.0.0:0 LISTENING 1416
TCP 0.0.0.0:1539 0.0.0.0:0 LISTENING 1892
TCP 0.0.0.0:1540 0.0.0.0:0 LISTENING 732
TCP 0.0.0.0:1569 0.0.0.0:0 LISTENING 724
TCP 0.0.0.0:5357 0.0.0.0:0 LISTENING 4
TCP 127.0.0.1:1575 127.0.0.1:48303 ESTABLISHED 2104
TCP 127.0.0.1:4000 0.0.0.0:0 LISTENING 11476 <= Oops
TCP 127.0.0.1:4012 0.0.0.0:0 LISTENING 10888
TCP 127.0.0.1:4013 0.0.0.0:0 LISTENING 10888
说明一下参数:
嫌疑人 Get,是一个 PID 为 11476 的进程占用了 4000 端口,除掉他之前先看看到底是谁?这个可以通过 tasklist /svc /FI "PID eq 11476"
搞定。
再说明一下参数:
以下是运行结果:
Image Name PID Service
========================= ======== ============================================
FoxitProtect.exe 11476 FxService
好的,我昨天刚刚安装了一个 FixitReader。停掉服务,一切搞定。
写个 Powershell 脚本吧:
param([string] $EndPointPattern)
$pids = netstat -aon | ?{ $_.contains("$EndPointPattern") } | %{ $_.split(' ', [system.stringsplitoptions]::RemoveEmptyEntries)[4] } | select -unique
$pids | %{ ps -Id $_ }
这里面没有考虑各种乱七八糟的情况,如果要用,请自行增补细节。好了,运行一下吧:
$ .\Search-EndPointProcess.ps1 -EndPointPattern "127.0.0.1:48303"
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id SI ProcessName
------- ------ ----- ----- ----- ------ -- -- -----------
1047 30 8516 5044 178 2104 0 svchost
不错嘛!:-D