Skip to content

Conversation

@JanithShimalka
Copy link

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR N/A

Description

Adds a new pm2 ports command that displays running processes with their port
information.

Motivation

In production environments running 10-20+ PM2 applications, it's difficult to quickly
identify which ports each application is using. This is particularly challenging with
microservices that may use message brokers (MQTT, RabbitMQ) instead of HTTP, making
traditional tools like netstat or lsof unreliable.

Changes

  • Add new pm2 ports command
  • Display process list with port column (similar to pm2 list)
  • Detect ports from: ecosystem config → CLI args → environment variables
  • Minimal implementation: 1 new file + 4 small modifications
  • No daemon changes (client-side computation only)

Port Detection Priority

  1. Ecosystem config: port or env.PORT
  2. CLI arguments: --port 3000, -p 3000, --http-port 8080
  3. Environment variables: PORT, HTTP_PORT, HTTPS_PORT, API_PORT,
    SERVER_PORT

Usage

# Start apps with port configuration
pm2 start ecosystem.config.js  # port from config
pm2 start app.js -- --port 3000  # port from CLI args
PORT=5000 pm2 start app.js       # port from env var

# Display ports
pm2 ports

Example Output

┌────┬───────────┬─────────┬───┬────────┬──────┬──────┬──────┐
│ id │ name      │ mode    │ ↺ │ status │ port │ cpu  │ mem  │
├────┼───────────┼─────────┼───┼────────┼──────┼──────┼──────┤
│ 0  │ api       │ cluster │ 0 │ online │ 3000 │ 0.1% │ 50mb │
│ 1  │ worker    │ fork    │ 2 │ online │ 8080 │ 2.3% │ 75mb │
│ 2  │ cron      │ fork    │ 0 │ online │ -    │ 0.0% │ 25mb │
└────┴───────────┴─────────┴───┴────────┴──────┴──────┴──────┘

Testing

- Ecosystem config with port
- CLI arguments (--port, -p, --http-port)
- Environment variables
- Cluster mode
- Multiple apps with mixed configurations
- Processes without ports (shows -)
- Multiple ports (shows comma-separated)
- All existing PM2 commands still work

Files Changed

- lib/API/UX/pm2-ports.js (new) - Port display logic
- lib/API/UX/index.js - Export ports module
- lib/API.js - Add ports() API method
- lib/binaries/CLI.js - Register command
- README.md - Add documentation

---

…2 ports' command to list processes with port info - Detects ports from: ecosystem config, CLI args, env variables - Priority: env.PORT > CLI --port > other env vars (HTTP_PORT, etc.) - Displays port column in table format similar to 'pm2 list' - Handles edge cases: multiple ports, missing ports, cluster mode - Minimal changes: 1 new file + 3 small modifications
@CLAassistant
Copy link

CLAassistant commented Dec 10, 2025

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants