🚀 3.14 Monitoring with PM2 – Keep Your Node.js App Alive & Healthy

Table of Contents

Imagine your Node.js app is like a robot 🤖 running on your computer.

What if it suddenly crashes? Or uses too much memory?

That’s where PM2 comes in — your app’s personal doctor 🩺 and bodyguard 🛡️!


🔍 What is PM2?

PM2 is a process manager for Node.js.

It helps you:

  • Keep your app always running (even if it crashes)
  • Restart it automatically when needed
  • Monitor memory and CPU usage
  • View logs easily
  • Handle multiple apps

🧠 Think of PM2 like a smart babysitter for your app.


🛠️ How to Install PM2

First, open your terminal and run:

bashCopyEditnpm install -g pm2

The -g flag installs PM2 globally so you can use it anywhere on your machine 🌎.


🚦 Starting Your App with PM2

Instead of using node app.js, use:

bashCopyEditpm2 start app.js

✅ Your app is now being watched by PM2 like a hawk 🦅.


🔄 Auto-Restart on Crashes

PM2 will auto-restart your app if it crashes. No manual fix needed 🙌.

Example:

bashCopyEditpm2 start server.js --watch

🧠 The --watch flag tells PM2 to restart your app if any file changes — super helpful for development!


📊 Monitor Your App’s Health

To see live performance:

bashCopyEditpm2 monit

This opens a dashboard like this (image placeholder):

Pm2 Monitoring Dashboard Screenshot Here
🚀 3.14 Monitoring With Pm2 - Keep Your Node.js App Alive & Healthy 2

You’ll see:

  • CPU Usage
  • Memory Usage
  • Restart counts

👀 It’s like looking at your app’s heartbeat ❤️.


📝 Viewing Logs with PM2

Logs are like your app’s diary 📖.

To see logs:

bashCopyEditpm2 logs

You’ll see:

  • Console outputs
  • Errors
  • Warnings

🔧 This is super helpful for debugging!


♻️ Log Rotation (So It Doesn’t Fill Your Disk!)

Logs can grow BIG. You don’t want your computer to explode 💣.

PM2 has a feature called log rotation.

Install the log rotate module:

bashCopyEditpm2 install pm2-logrotate

You can also configure it:

bashCopyEditpm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 5

✅ This keeps your logs clean and small.


🔄 Auto Start on Reboot

Want your app to start automatically when your server reboots?

Just run:

bashCopyEditpm2 startup

Then follow the instructions PM2 gives you.

Save your running apps:

bashCopyEditpm2 save

💡 Now even after a power cut, your app is back up like magic 🔌✨!


🧼 Stopping & Managing Apps

Some useful PM2 commands:

CommandWhat it does
pm2 listShows all running apps
pm2 stop app_nameStops an app
pm2 restart app_nameRestarts an app
pm2 delete app_nameRemoves app from PM2

💬 Example:

bashCopyEditpm2 stop app.js

📦 Deploying with PM2 (Bonus)

You can even use PM2 to deploy code to remote servers.

It supports ecosystem files and zero-downtime reloads.

But that’s a topic for another guide! 😉 Stay tuned.


🧠 Why Use PM2?

✅ Keeps your app running
✅ Easy logging
✅ Auto-restart on crash
✅ Health monitoring
✅ Works great in production

Perfect tool for developers — even beginners!


🧾 Summary

FeatureBenefit
Auto-RestartKeeps app alive always
LogsSee errors, output
MonitoringView live CPU/Memory stats
Log RotationSaves disk space
Startup ScriptRuns app after reboot

📌 Final Tips

  • Always use pm2 save after starting apps
  • Use pm2 logs to debug issues
  • Monitor your app regularly with pm2 monit

📣 Like This Tutorial? Follow Web Codder 👇

If you enjoyed this friendly tech guide, you’ll love our video tutorials and coding tips!

👉 Subscribe on YouTube: Web Codder
📸 Follow on Instagram: @web_codder_official
💬 Join our WhatsApp Community: webcodder.dev/whatsapp

Let’s learn and build cool stuff together! 💻🔥

Share the Post:
Picture of Web Codder

Web Codder

Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.

Related Posts