Header Ads

Step-by-Step Guide to Monitoring Node.js Applications in Production with PM2, Logrotate, and Alerts

 Step-by-Step Guide to Monitoring Node.js Applications in Production with PM2, Logrotate, and Alerts

Running a Node.js application in production isn’t just about deploying your code—it’s about keeping it alive, responsive, and observable. In this comprehensive guide, you’ll learn how to monitor and maintain your Node.js app using PM2, Logrotate, and alerts for reliability and performance.



Why Monitoring Is Critical for Node.js Apps

  • Prevent crashes & downtime
  • Auto-restart on failure
  • Control log file sizes
  • Receive alerts proactively
  • Track memory & CPU usage

Tools You'll Use
Tool                                            Purpose
PM2                                           Process management and monitoring
Logrotate                                  Log file rotation and cleanup
Alerts                                        Real-time notifications

Step 1: Install and Start PM2

PM2 is a powerful process manager that will daemonize your Node.js application.

npm install pm2 -g


Start your Node.js app with PM2: 

pm2 start app.js --name ionic-backend

Save process list:

pm2 save

Enable auto-start on server reboot:

pm2 startup

Copy and run the command PM2 gives you after pm2 startup.


Step 2: Monitor in Real-Time with PM2

To track CPU, memory, and uptime for all processes:

pm2 monit

Or for detailed info:

Or for detailed info:


Step 3: Manage Logs with Logrotate

Your app’s logs will grow rapidly. Let’s rotate and compress logs to avoid disk overflow.

Find PM2 logs:

ls ~/.pm2/logs/

Install Logrotate:

sudo apt install logrotate

Create a logrotate config for PM2:

sudo nano /etc/logrotate.d/pm2

Paste the following:

/home/YOUR_USERNAME/.pm2/logs/*.log {

    daily

    missingok

    rotate 7

    compress

    delaycompress

    notifempty

    copytruncate

}


This will rotate logs daily, keep 7 compressed backups, and truncate logs in-place.


Test logrotate: 

sudo logrotate -f /etc/logrotate.d/pm2


Step 4: Set Up Alerts

Option 1: Use PM2 Plus (Recommended for simple setups)

pm2 plus

You can configure:

  • Crash alerts
  • Memory threshold warnings
  • CPU spike alerts

Option 2: Use External Services

🔹 Uptime Robot or BetterUptime
  • Free ping monitoring + email/Slack alerts
  • Add your API or app URL
🔹 Log Monitoring: Logtail, Loggly, ELK
  • Parse logs for errors
  • Alert on specific patterns
🔹 Server Monitoring: Grafana + Prometheus
  • Advanced monitoring with dashboards and threshold-based alerts
 

Step 5: Simulate and Test

  • Kill the process and check if PM2 auto-restarts:
pkill node

  • Fill logs and confirm that logrotate compresses them
  • Check alerts by exceeding memory (simulate with a memory leak or stress tool

 

Summary

By setting up proper monitoring, your Node.js app will stay reliable, self-healing, and production-grade


 Related Guides

Post a Comment

0 Comments