How to list all processes/services running on different ports
Asked Answered
Z

5

7

Is there a command that lists all the services that are running on different ports of localhost?

In my case, when I'm working on an Angular app, I may run it on localhost:4200, a React app on localhost:3000 and a Redis server on localhost:6379, etc.

Is there a way of knowing if these are running and how can I kill/stop them?

Zumwalt answered 6/1, 2020 at 16:40 Comment(2)
Have you tried to look that up using Google? That directly lists stuff like superuser.com/questions/529830/…Kenney
I did search for some time, I think I missed the keyword "Open ports".Zumwalt
A
2

On windows use netstat -nba | FINDSTR "LISTEN" to get a list of processes (Pids) listening on a port

if you need to find a specific port, then pipe it through findstr twice netstat -nba | FINDSTR "LISTEN" | FINDSTR "3000"

In powershell you can then use Stop-Process CMDlet with the Id option to stop the process

Stop-Process -Id 1234

if you want to do it all in one powershell command, you can go with

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

or

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

for redis

Atelier answered 6/1, 2020 at 16:42 Comment(6)
This isn't working for me. It's not doing anything.Zumwalt
on windows? What about just netstat -baAtelier
Yeah, must be adminisrtratorAtelier
How can know the name or any other detail of the processes along with their ports?Zumwalt
you can get more info with Get-Process -Id (Get-NetTCPConnection -LocalPort 6379).OwningProcessAtelier
I don't know if this changed in newer windows versions, or it was just an oversight, but you also need to specify the -o flag to get the PID. so that would be: netstat -nbao | FINDSTR "LISTEN"Quillet
R
9

Which operating system are you using? The answer may differ depending on the type of the operating system, including different distributions.

For example, on some Linux distributions I'd rather use ss -nltp.

Example:

$ ss -nltp
State      Recv-Q Send-Q   Local Address:Port          Peer Address:Port
LISTEN     0      128                  *:22                       *:*   
LISTEN     0      10           127.0.0.1:25                       *:*   
LISTEN     0      128                 :::111                     :::*   
LISTEN     0      50                  :::8080                    :::*   
LISTEN     0      128                 :::22                      :::*   

Explained:

-n, --numeric don't resolve service names (google.com --> 1.2.3.4)

-l, --listening display listening sockets (just the ports you're listening at)

-p, --processes show process using socket (include sub processes locking sockets)

-t, --tcp display only TCP sockets

A more general command would be netstat.

Example: $ netstat -nl

Please check the manual of ss for more information.


Edit: Since you said you were using Windows, you can use this to list all the relevant processes (-n == numeric, -a == all, -o == show process id, -p TCP == show TCP only):

netstat -nao -p TCP

Last column would be the process ID, you can use taskkill to kill the process:

taskkill /F /PID <PID>

Where /F says forcefully kill and /PID indicates the next value is the process ID.

Rhodarhodamine answered 6/1, 2020 at 16:56 Comment(1)
The following lets you select processes by their name or the port they're using and send SIGTERM to them: kill $(ss -nap | fzf | grep -oP 'pid=\K[^,]+')Nodal
A
2

On windows use netstat -nba | FINDSTR "LISTEN" to get a list of processes (Pids) listening on a port

if you need to find a specific port, then pipe it through findstr twice netstat -nba | FINDSTR "LISTEN" | FINDSTR "3000"

In powershell you can then use Stop-Process CMDlet with the Id option to stop the process

Stop-Process -Id 1234

if you want to do it all in one powershell command, you can go with

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

or

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

for redis

Atelier answered 6/1, 2020 at 16:42 Comment(6)
This isn't working for me. It's not doing anything.Zumwalt
on windows? What about just netstat -baAtelier
Yeah, must be adminisrtratorAtelier
How can know the name or any other detail of the processes along with their ports?Zumwalt
you can get more info with Get-Process -Id (Get-NetTCPConnection -LocalPort 6379).OwningProcessAtelier
I don't know if this changed in newer windows versions, or it was just an oversight, but you also need to specify the -o flag to get the PID. so that would be: netstat -nbao | FINDSTR "LISTEN"Quillet
L
1

You can use batch(cmd.exe) for this task

::List processes
@Tasklist.exe

::List services
Sc.exe Query Type= service

::Stop a process 
Taskkill.exe /im "Image name of a task.exe"


::Stop a service
@%__APPDIR__%Net.exe Stop "Service name"

::Start a service
@%__APPDIR__%Net.exe Start "Service name"

To execute these bat files:

  • Open notepad and copy one of the codes of them for the task specified.
  • Save it as a file with .bat extension.
  • Open cmd.exe and drag and drop the bat file and execute it.
Lisalisabet answered 6/1, 2020 at 16:52 Comment(1)
A simple command like MikNiller's answer seems better. Are there any differences in the result of using this approach.Zumwalt
H
0

Try following command :

sudo netstat -ltnp

The above command gives netstat information based on the following features:

l: display only listening sockets 
t: display tcp connection 
n: display addresses in a numerical form 
p: display process ID/ Program name

Your output should look something like :

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1784/sshd       
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      9031/cupsd      
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      1504/beam.smp   
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      1798/postgres   
tcp        0      0 0.0.0.0:7070            0.0.0.0:*               LISTEN      1245/anydesk    
tcp        0      0 0.0.0.0:70              0.0.0.0:*               LISTEN      1803/nginx -g daemo
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      1504/beam.smp   
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      1476/mongod     
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1739/mysqld     
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      1683/redis-server 1
tcp        0      0 127.0.0.1:33485         0.0.0.0:*               LISTEN      5582/chrome --type=
tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN      1736/epmd       
tcp        0      0 127.0.0.1:5939          0.0.0.0:*               LISTEN      2435/teamviewerd
tcp        0      0 127.0.0.1:21460         0.0.0.0:*               LISTEN      15337/node      
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      4671/dnsmasq    
tcp6       0      0 :::22                   :::*                    LISTEN      1784/sshd       
tcp6       0      0 ::1:631                 :::*                    LISTEN      9031/cupsd      
tcp6       0      0 127.0.0.1:5563          :::*                    LISTEN      15337/node      
tcp6       0      0 :::5672                 :::*                    LISTEN      1504/beam.smp   
tcp6       0      0 :::80                   :::*                    LISTEN      2532/apache2    
tcp6       0      0 :::4369                 :::*                    LISTEN      1736/epmd     
Heffernan answered 14/6, 2020 at 7:57 Comment(0)
M
0

List all the processes running on windows with their PORTS, run this command onthe cmd:

netstat -ano | find "LISTEN"
Mustache answered 7/4, 2024 at 4:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.