【问题标题】:Find the PID of a process that uses a port on Windows查找在 Windows 上使用端口的进程的 PID
【发布时间】:2013-04-03 21:03:09
【问题描述】:

我的服务在使用经典启动时崩溃:

java.rmi.server.ExportException: Listen failed on port: 9999

如何找到杀死它的进程?

【问题讨论】:

标签: port


【解决方案1】:

PowerShell(与核心兼容)单行来简化复制粘贴场景:

netstat -aon | Select-String 8080 | ForEach-Object { $_ -replace '\s+', ',' } | ConvertFrom-Csv -Header @('Empty', 'Protocol', 'AddressLocal', 'AddressForeign', 'State', 'PID') | ForEach-Object { $portProcess = Get-Process | Where-Object Id -eq $_.PID; $_ | Add-Member -NotePropertyName 'ProcessName' -NotePropertyValue $portProcess.ProcessName; Write-Output $_ } | Sort-Object ProcessName, State, Protocol, AddressLocal, AddressForeign | Select-Object  ProcessName, State, Protocol, AddressLocal, AddressForeign | Format-Table

输出:

ProcessName State     Protocol AddressLocal AddressForeign
----------- -----     -------- ------------ --------------
System      LISTENING TCP      [::]:8080    [::]:0
System      LISTENING TCP      0.0.0.0:8080 0.0.0.0:0

相同的代码,对开发人员友好:

$Port = 8080

# Get PID's listening to $Port, as PSObject
$PidsAtPortString = netstat -aon `
  | Select-String $Port
$PidsAtPort = $PidsAtPortString `
  | ForEach-Object { `
      $_ -replace '\s+', ',' `
  } `
  | ConvertFrom-Csv -Header @('Empty', 'Protocol', 'AddressLocal', 'AddressForeign', 'State', 'PID')

# Enrich port's list with ProcessName data
$ProcessesAtPort = $PidsAtPort `
  | ForEach-Object { `
    $portProcess = Get-Process `
      | Where-Object Id -eq $_.PID; `
    $_ | Add-Member -NotePropertyName 'ProcessName' -NotePropertyValue $portProcess.ProcessName; `
    Write-Output $_;
  }

# Show output
$ProcessesAtPort `
  | Sort-Object    ProcessName, State, Protocol, AddressLocal, AddressForeign `
  | Select-Object  ProcessName, State, Protocol, AddressLocal, AddressForeign `
  | Format-Table

【讨论】:

    【解决方案2】:

    命令:

    netstat -aon | findstr 4723
    

    输出:

    TCP    0.0.0.0:4723           0.0.0.0:0                LISTENING       10396
    

    现在使用 Windows 中的for 命令剪切进程 ID“10396”。

    命令:

    for /f "tokens=5" %a in ('netstat -aon ^| findstr 4723') do @echo %~nxa
    

    输出:

    10396

    如果你想削减第 4 个数字的值表示“LISTENING”,那么在 Windows 中使用命令。

    命令:

    for /f "tokens=4" %a in ('netstat -aon ^| findstr 4723') do @echo %~nxa
    

    输出:

    聆听

    【讨论】:

    • 对于过滤唯一 PID 的任何建议,因为该命令有时会多次返回相同的进程?
    【解决方案3】:

    在修改了一些脚本后,我开始了这个动作。复制并保存在 .bat 文件中:

    FOR /F "usebackq tokens=5" %%i IN (`netstat -aon ^| find "3306"`) DO taskkill /F /PID %%i
    

    在需要空闲的端口号中更改'find "3306"'。然后以管理员身份运行该文件。它将杀死该端口上运行的所有进程。

    【讨论】:

      【解决方案4】:

      查找在 Windows 上使用端口的进程的 PID (例如端口:“9999”)

      netstat -aon | find "9999"
      

      -a 显示所有连接和监听端口。

      -o 显示与每个连接关联的拥有进程 ID。

      -n 以数字形式显示地址和端口号。

      输出:

      TCP    0.0.0.0:9999       0.0.0.0:0       LISTENING       15776
      

      然后按PID杀死进程

      taskkill /F /PID 15776
      

      /F - 指定强制终止进程。

      注意:您可能需要额外的权限(由管理员运行)才能杀死某些进程

      【讨论】:

      • 为什么 netstat 不退出,除非你给它 n 标志?
      • @JaredBeach - 它正在等待反向 DNS 名称解析,因此它最终会在超时完成后完成。如果这在 internal IP 上挂起,则表明您的本地 DNS 服务器存在问题。
      【解决方案5】:

      只需打开一个命令外壳并输入(假设您的端口是 123456):

      netstat -a -n -o | find "123456"
      

      你会看到你需要的一切。

      标题是:

       Proto  Local Address          Foreign Address        State           PID
       TCP    0.0.0.0:37             0.0.0.0:0              LISTENING       1111
      

      【讨论】:

      • 这里我如何只获取 pid,因为它会返回完整的详细信息
      • 如何获取结果上方唯一的PID形式
      • 或者,nestat -aon | findstr 123456
      • 对于 windows/cygwin,它可能是 netstat -a -n -o | grep“123456”
      • 为了更精确的结果,在端口号前面加一个冒号:find ":123456"
      【解决方案6】:

      如果您想以编程方式执行此操作,您可以使用 PowerShell 脚本中提供的一些选项,如下所示:

      $processPID =  $($(netstat -aon | findstr "9999")[0] -split '\s+')[-1]
      taskkill /f /pid $processPID
      

      但是;请注意,您越准确,您的 PID 结果就越精确。如果您知道端口应该在哪个主机上,您可以缩小很多范围。 netstat -aon | findstr "0.0.0.0:9999" 只会返回一份申请,而且很可能是正确的一份。仅搜索端口号可能会导致您返回仅包含 9999 的进程,如下所示:

      TCP    0.0.0.0:9999                        0.0.0.0:0       LISTENING       15776
      UDP    [fe80::81ad:9999:d955:c4ca%2]:1900  *:*                             12331
      

      最有可能的候选人通常首先结束,但如果进程在您运行脚本之前结束,您可能会以 PID 12331 结束并杀死错误的进程。

      【讨论】:

        【解决方案7】:

        这有助于使用端口号查找 PID。

        lsof -i tcp:port_number
        

        【讨论】:

        • 输入命令后我得到'lsof' is not recognized as an internal or external command.
        • 在 Linux 中工作。
        • 这个问题是关于Windows的
        猜你喜欢
        • 2023-01-21
        • 1970-01-01
        • 2020-12-21
        • 1970-01-01
        • 2011-09-17
        • 2020-08-09
        • 2021-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多