Find the PID of a process that uses a port on Windows

前端 未结 7 1188
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 16:58

My service crash on startup with the classic:

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

How can I find the process for k

相关标签:
7条回答
  • 2020-11-30 17:22

    Find the PID of a process that uses a port on Windows (e.g. port: "9999")

    netstat -aon | find "9999"
    

    -a Displays all connections and listening ports.

    -o Displays the owning process ID associated with each connection.

    -n Displays addresses and port numbers in numerical form.

    Output:

    TCP    0.0.0.0:9999       0.0.0.0:0       LISTENING       15776
    

    Then kill the process by PID

    taskkill /F /PID 15776
    

    /F - Specifies to forcefully terminate the process(es).

    Note: You may need an extra permission (run from administrator) to kill some certain processes

    0 讨论(0)
  • 2020-11-30 17:24

    Just open a command shell and type (saying your port is 123456):

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

    You will see everything you need.

    The headers are:

     Proto  Local Address          Foreign Address        State           PID
     TCP    0.0.0.0:37             0.0.0.0:0              LISTENING       1111
    
    0 讨论(0)
  • 2020-11-30 17:24

    PowerShell (Core-compatible) one-liner to ease copypaste scenarios:

    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
    

    Output:

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

    Same code, developer-friendly:

    $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
    
    0 讨论(0)
  • 2020-11-30 17:35

    Command:

    netstat -aon | findstr 4723
    

    Output:

    TCP    0.0.0.0:4723           0.0.0.0:0                LISTENING       10396
    

    Now cut the process ID, "10396", using the for command in Windows.

    Command:

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

    Output:

    10396

    If you want to cut the 4th number of the value means "LISTENING" then command in Windows.

    Command:

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

    Output:

    LISTENING

    0 讨论(0)
  • 2020-11-30 17:37

    This helps to find PID using port number.

    lsof -i tcp:port_number
    
    0 讨论(0)
  • 2020-11-30 17:40

    After some fiddling with a script I came to this action. Copy and save it in a .bat file:

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

    Change 'find "3306"' in the port number which needs to be free. Then run the file as administrator. It will kill all the processes running on this port.

    0 讨论(0)
提交回复
热议问题