skip to Main Content

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?

4

Answers


  1. 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

    Login or Signup to reply.
  2. 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.
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. 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     
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search