Skip to content

PM2 vs a PaaS-native process supervisor: choosing the right boundary

Running PM2 inside a PaaS container often duplicates work the platform already does — and hides crashes from the platform's restart logic. Here is when PM2 is the right tool and when it is one supervisor too many.

You inherit a Node.js application running on a managed PaaS. The repository contains an ecosystem.config.js with PM2 configured for cluster mode, log rotation, and graceful reloads. The platform itself also has a process supervisor that monitors the container, restarts on exit, ships logs, and rolls workers during deploys. The application has been crashing twice a week and the platform’s restart counter shows zero — because PM2 catches the crash, restarts the worker silently, and the platform sees a healthy container the whole time. This is the symptom of having two supervisors stacked. The fix is not to “fix PM2 logging” — it’s to decide which layer owns process management and remove the other.

What’s happening

PM2 was designed for the era when Node ran on long-lived VMs that you provisioned and forgot. It owns the process lifecycle: forks workers, watches their exits, rotates logs, and exposes a CLI for pm2 reload graceful restarts. Modern PaaS environments — Jelastic, App Service, Heroku-likes, container platforms — each have their own equivalent: the platform’s supervisor watches the container PID 1, restarts it on exit, ships stdout/stderr to a log aggregator, and runs the rollout strategy during deploys. Both layers solve the same problem.

Three things go wrong when both supervisors are live.

PM2 absorbs crashes the platform should see. If a Node worker crashes with an uncaught exception, PM2 catches it, restarts the worker in place, and the container’s PID 1 (PM2 itself) keeps running. The platform’s restart metric — which most teams alert on — stays at zero. The crash never appears in the platform dashboard. You discover it weeks later because a customer reports an inconsistent response, and the trail is in PM2’s local log file, which the platform’s log aggregator doesn’t read.

Cluster forking happens twice. PM2’s cluster mode forks N workers. The platform may also fork or scale horizontally based on CPU. You end up with platform_nodes × pm2_workers Node processes, each one a copy of the application memory. On a 2 GB container with PM2 running 4 workers, the application has 4× the per-process memory; if the platform then horizontally scales to 3 containers, that’s 12 Node processes total. The horizontal scaler sees the per-container memory at 60% and thinks the application is comfortable, while a single container with 1 worker would have used 200 MB and the platform could have run fewer nodes.

Deploy rollouts get confusing semantics. A platform-native deploy strategy might say “rolling: drain one container, replace it, wait for health, drain the next.” PM2’s reload does the same dance inside one container, gracefully cycling workers. Stacking them means a deploy first drains containers (platform), then within each new container PM2 spawns workers one by one (PM2). For most applications this works; for applications with long-running connections or in-process state, the boundary gets blurry — which is what causes the “we deployed and the WebSocket clients all disconnected, but the canary said clean” incident.

When PM2 is still the right answer: the platform’s process supervisor is missing or weak, you genuinely need PM2’s specific features (the metrics agent, the keymetrics dashboard, the deploy CLI for non-containerized targets), or the platform is “fixed-size container, run whatever you want inside.” On a properly-managed PaaS, the first case is rare. On Jelastic and similar, the platform supervisor is competent; PM2 inside the container is extra weight.

The decision rule: if you can describe the platform’s deploy strategy, restart policy, and log shipping in one sentence and they are the behaviors you want, use the platform-native supervisor and run Node directly. If the platform is more of a “here’s a container, you handle it” runtime, PM2 is a reasonable choice — but configure it to log to stdout, not to local files the platform won’t read.

How to verify

Look at PID 1 inside the container. If the answer is PM2 (or PM2’s God Daemon), PM2 is the supervisor:

ps -o pid,comm -p 1
# PID  COMMAND
#   1  PM2 v5.3.0: God

If PID 1 is node or the platform’s bootstrap process, PM2 is not in the supervision chain even if it’s installed.

Whether PM2 is silently restarting workers:

pm2 status
# id  name       status   ↺      cpu   mem
#  0  api        online   47     12%   180mb

The ↺ column is the restart count. If it’s not zero on an application that has been running for hours, PM2 is restarting workers and the platform doesn’t know.

Whether the platform’s logs reflect what’s actually happening:

# Tail the platform-side log (path varies by platform)
tail -f /var/log/platform/app/stdout.log

# Compare to PM2's local log
tail -f ~/.pm2/logs/api-out.log ~/.pm2/logs/api-error.log

If errors appear in PM2’s log but not in the platform log, PM2 is writing to local files and the aggregator isn’t reading them. Either reconfigure PM2 to log to stdout (--no-daemon mode in some configurations), or drop PM2.

The fix

If you’re keeping PM2 — typically because you depend on its cluster mode or graceful reload — configure it to play well with the platform. Run in --no-daemon mode so PM2 is the foreground process (and therefore the container’s PID 1), and route logs to stdout so the platform’s aggregator sees them. The ecosystem.config.js:

module.exports = {
  apps: [{
    name: 'api',
    script: './server.js',
    instances: parseInt(process.env.WEB_CONCURRENCY, 10) || 'max',
    exec_mode: 'cluster',
    max_memory_restart: '1G',
    kill_timeout: 10000,
    listen_timeout: 8000,
    wait_ready: true,
    out_file: '/dev/stdout',
    error_file: '/dev/stderr',
    merge_logs: true,
    autorestart: true,
    max_restarts: 10,
    min_uptime: '30s',
    exp_backoff_restart_delay: 1000,
    env: {
      NODE_ENV: 'production',
    },
  }],
};

And the start command — note pm2-runtime, not pm2 start. pm2-runtime is PM2 designed for containers: foreground, logs to stdout, exits when the app exits so the platform sees the crash:

{
  "scripts": {
    "start": "pm2-runtime start ecosystem.config.js"
  }
}

With this configuration, the platform sees PM2 as the container’s main process; if all workers crash and PM2 gives up after max_restarts, the container exits and the platform restarts it — which is the behavior you want. Logs flow to the platform’s aggregator. The supervisor stack is still two layers, but they’re coupled correctly.

If you’re removing PM2 — usually the right call on a managed PaaS — replace cluster mode with a small native cluster wrapper (see the Node.js cluster mode note) and let the platform handle restarts, logs, and rollouts. The package.json:

{
  "scripts": {
    "start": "node server.js"
  },
  "engines": {
    "node": ">=20.0.0 <21.0.0"
  }
}

For graceful shutdown — the one feature PM2 gives you for free — handle SIGTERM in server.js directly:

const server = app.listen(process.env.PORT || 3000);

let shuttingDown = false;
process.on('SIGTERM', () => {
  if (shuttingDown) return;
  shuttingDown = true;
  console.log('SIGTERM received, draining');
  server.close((err) => {
    if (err) { console.error(err); process.exit(1); }
    process.exit(0);
  });
  // Hard cap: platforms typically give 30s before SIGKILL
  setTimeout(() => process.exit(1), 25000).unref();
});

The platform-native supervisor will send SIGTERM, wait for the configured grace period (often 30s), then SIGKILL. Honoring SIGTERM with server.close() finishes in-flight requests, then exits cleanly — the same graceful behavior PM2 provided, owned by application code.


Picking the supervisor boundary, sizing workers, and wiring deploy strategy on PaaS is exactly what we cover under Cloud PaaS plus deployment management — book a scoping call.