Where can I find the ports of all running pm2 apps?
Asked Answered
W

6

36

I have a server with PM2 installed and 10 running node apps. Every App should run with a different port number. When I install a new App on the server I need the information about the used ports. With 'pm2 list' I get much info about the apps but not the port.

pm2 list

App name       │ id │ version │ mode │ pid   │ status │ restart │ uptime │ cpu  │ mem        │ user │ watching
example_name   │ 1  │ 0.0.0   │ fork │ 25651 │ online │ 0       │ 37D    │ 0%   │ 386.3 MB   │ root │ disabled

I can not find a overview of all used ports and I can't believe that this important information is not given by PM2. Does anyone have any idea where I see a list with all used ports in PM2?

Weaverbird answered 16/11, 2018 at 10:36 Comment(3)
please guide, did you find any solution?Contango
Solution is to move to docker :) PM2 is not well maintained anymore.Weaverbird
use ntlp and add alias for permanent solutions, stackoverflow.com/a/78764354Piecrust
S
25

Yeah this is a bit of a failing with pm2 IMHO. Only when you have more than one instance (site) running on the server. I use:

ss -tnlp | grep "node /"

You can then eyeball the pid from pm2 ls 12th column and the port, or in my case you get just a snippet of the directory it's running from. UPDATE: you can use this monstrosity:

ss -ntlp | grep $(pm2 ls | grep "SITENAME" | awk '{print $12}') | awk '{print $4}'

Which dumps the port out.

The OP added a comment saying he added the port number into the name of the running node app, which could get messy, but is a good idea.

Shipworm answered 4/12, 2019 at 13:32 Comment(1)
It wouldn't work for me, after changing it to this ss -ntlp | grep $(pm2 ls | grep "ShellAPI" | awk '{print $12}') it worked successfullySkyline
K
18

Most of the time the ports are visible in the logs. Try this:

pm2 logs

The source code of most applications logs the exposed port when the app is running. This is very helpful indeed to find all the ports of running pm2 apps.

Kentigera answered 11/8, 2022 at 16:50 Comment(2)
It was quickestFortress
The other solutions don't work on windows. In a node.js project I run this npx pm2 logs. I even added it to package.json scripts "log": "pm2 logs",Melitta
E
11
sudo netstat -tnlp
  • -t for TCP only
  • -l for Listening ports
  • -n for Don't look up service & names, just display numbers
  • -p for show processor information
Eraeradiate answered 15/7, 2022 at 0:36 Comment(0)
D
9

Hi Schmidko even i tried the same but i also did not found such option in pm2 so i am currently getting the pid from pm2 l and then using the below command to get port on my linux os

sudo netstat -ano -p tcp | grep <PID>

so i get output like this : tcp6 0 0 :::1111 :::* LISTEN 2111/app.js off (0.00/0/0)

where 2111/app.js is PID & :::1111 is the port

(posting a comment here as i dont have right to comment)

Denims answered 8/8, 2019 at 6:37 Comment(2)
I solved the problem with adding the port number to the app name :) Your solution is a little bit to complicated when there are running 20 apps on a server. But tumbs up for this approach. :)Weaverbird
i liked your idea too :) Just waiting for pm2 too add this feature may be on pm2 show commandDenims
P
1

Use .bashrc alias function.

Add this function in your ~/.bashrc

pm2_ports() {
  printf "%-20s %-10s\n" "App Name" "Port"
  printf "%-20s %-10s\n" "--------" "----"
  for app in $(pm2 jlist | jq -r '.[] | @base64'); do
    _jq() {
      echo ${app} | base64 --decode | jq -r ${1}
    }
    name=$(_jq '.name')
    pid=$(_jq '.pid')
    ports=$(ss -ntlp | grep $pid | awk '{print $4}')
    for port in $ports; do
      printf "%-20s %-10s\n" "$name" "$port"
    done
  done
}

Run source ~/.bashrc

Run pm2_ports command will give table output for you.

For Single application you can use:

ss -ntlp | grep $(pm2 ls | grep "process-name" | awk '{print $12}') | awk '{print $4}'

Piecrust answered 18/7 at 12:13 Comment(0)
F
0

I tend to use the following commands:

ps aux | grep node

Will show the running node processes and their PID

netstat -atlnp | grep LISTEN | grep node

Will show all ports running node (not just from PM2 if running other node applications outside of pm2)

You can then cross reference the PID of the node process to its port that it is listening on.

Fishbein answered 3/9, 2022 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.