If you run Node apps with PM2, you know pm2 list. It shows the id, name, status, CPU and memory of every process. What it does not show is the one thing you often need on a server: which port each app is listening on.
I hit this again while checking my own server. Several apps, a reverse proxy in front of them, and no quick way to map a process to a port. So I wrote a small tool and published it.
$ pm2-ports
id name status pid ports env PORT
──────────────────────────────────────────────────
1 pm2-logrotate online 69838 - -
0 website online 368112 3000 3000
The usual workarounds, and why they are annoying
ss -ltnporlsof -ishows ports and PIDs, but you then have to match each PID to a PM2 name by hand.pm2 describe <name>shows the environment, but only thePORTvalue PM2 passed in. If the app ignores it and uses a hard-coded port, that value is simply wrong.Reading the code or config works until someone changes it.
What I wanted was one table: PM2 name, PID, and the ports the kernel says are open.
How it works
pm2-ports does three things:
Reads the process list from PM2 (
pm2 jlist).Reads listening TCP sockets with
ss, falling back tonetstat, thenlsof, whichever the server has.Walks the process tree, so ports opened by child processes are attributed to the right PM2 process. This matters for apps like Next.js, where the process that listens is not always the one PM2 started directly.
It also shows the PORT environment variable next to the real ports. If the two differ, the app does not read process.env.PORT and the port is hard-coded.
There are no runtime dependencies. The programs are run without a shell, with fixed arguments.
Install and use
Run it once without installing:
npx pm2-ports
Or install it globally:
npm install -g pm2-ports
Then:
pm2-ports # online processes
pm2-ports --all # include stopped and errored processes
pm2-ports --json # machine-readable output
Requirements: Node.js 18+, PM2, and one of ss, netstat or lsof. It works on Linux and macOS. Windows is not supported.
Using it in scripts
The JSON output makes monitoring checks short. For example, list processes that are online but listening on nothing:
pm2-ports --json | jq '.[] | select(.status == "online" and (.ports | length == 0)) | .name'
That is a handy sanity check after a deploy: the process is "online" in PM2, but did it actually bind its port?
A few gotchas
PM2 is per user. Each Linux user has their own PM2 daemon. Run the tool as the user that runs PM2, for example
sudo -u deploy pm2-ports.Run as root if ports show
-everywhere. Without root,ssandnetstatdo not show which process owns another user's sockets.Servers without internet access: build a tarball with
npm pack pm2-portson a machine with npm access, copy it over, and install it from the file.
Links
It is MIT licensed. If you run PM2 in production and something is missing from that table, open an issue or send a PR.
