How to Fix High CPU Usage and Crash Loops in Node.js / PM2 Applications Print

  • High Server Load, CPU Load, WHM Load, highload
  • 0

If your website or API is loading slowly, throwing 502 Bad Gateway or 503 Service Unavailable errors, or your cPanel resource monitor shows maximum CPU usage, your application might be trapped in an automated crash-and-restart loop.

This guide will explain why this happens and show you how to resolve it via your terminal.

Why is My App Using Too Much CPU?

When you deploy a Node.js application using a process manager like PM2, the manager is designed to keep your app alive forever. If your code crashes due to an error, PM2 catches the crash and instantly attempts to restart it.

If there is a fundamental error in your startup script or command configurations, the application will crash instantly on launch, and PM2 will try to restart it every few seconds. This endless crash-and-restart loop consumes massive server processing power, driving up your CPU metrics and causing site lag.

How to Diagnose and Fix the Loop

Follow these steps via SSH terminal to isolate and fix the bottleneck.

Step 1: Check Your Live Application Status

Log in to your account via SSH and look at your active PM2 processes to check their restart counts and resource health:

Bash
 
pm2 list

Look at the (Restarts) and status columns. If you notice an application showing a high or rapidly climbing restart number alongside a red errored or cycling status, that specific application is causing your server load.

Step 2: Stop the Active Loop

To give your server immediate breathing room and drop the CPU utilization down to zero, tell PM2 to stop trying to restart the broken script:

Bash
 
# Stop the looping application (Replace "app-name" with your actual PM2 name or ID)
pm2 stop app-name

# Completely clear it out of PM2's active memory queue
pm2 delete app-name

Your account's resource metric should stabilize immediately within 15–30 seconds of running this.

Step 3: Find the Root Cause Inside the Logs

Because the application was spinning up and crashing too fast, we need to inspect the underlying framework logs to see what error code it was throwing. Run the following command to print out the latest runtime error stack traces:

Bash
 
pm2 logs app-name --err --lines 50

Common Root Causes to Look For:

  • Missing package.json Scripts: If you started your application using pm2 start npm -- start but forgot to define a "start" script block inside your project's package.json file, the runtime environment will immediately fail.

  • Jumbled Command Arguments: Passing complex arguments directly into a raw binary line can cause npm to misinterpret flags (e.g., throwing error exceptions like No workspaces found).

  • Port Already in Use: If another process or previous zombie thread is already listening on your application's designated environment port, your script will throw an EADDRINUSE exception and crash.

The Correct Way to Restart Your Application

To prevent arguments from getting jumbled up inside npm wrapper boundaries, the safest and most resource-efficient way to launch your Node.js application under PM2 is by pointing directly to your primary JavaScript initialization script (e.g., app.js, index.js, or server.js).

Navigate to your web directory (public_html) and launch the daemon safely using clean script isolation:

Bash
 
# 1. Navigate to your app directory
cd ~/public_html

# 2. Make sure all dependencies are cleanly optimized
npm install

# 3. Start the process directly by referencing the main JS file
pm2 start app.js --name "my-app"

# 4. Save the configuration state so it survives system reboots
pm2 save

Was this answer helpful?

« Back