Wednesday, December 10, 2025

thumbnail

Using PM2 for Node Process Management

 Using PM2 for Node.js Process Management

PM2 is a production-level process manager for Node.js applications. It ensures your app stays running, restarts automatically if it crashes, handles logs, and can scale across CPU cores — all with simple commands.

Think of PM2 as a tool that keeps your Node.js backend alive, stable, and scalable.

๐Ÿ”น 1. What PM2 Does

PM2 helps with:

Keeping the app always running (auto-restart)

Monitoring CPU & RAM usage

Load balancing across multiple CPU cores (“cluster mode”)

Easy logs & error tracking

Zero-downtime deployments

Startup scripts so PM2 launches at boot

Built-in dashboard (pm2 monit)

๐Ÿ”น 2. Install PM2

Install globally:

npm install -g pm2

Check version:

pm2 -v

๐Ÿ”น 3. Starting a Node.js App with PM2

Start an app:

pm2 start app.js

Start with a name:

pm2 start app.js --name api-server

Start with environment variables:

pm2 start app.js --name api-server --env production

๐Ÿ”น 4. Managing Processes

List running apps:

pm2 list

View detailed info:

pm2 describe api-server

Stop an app:

pm2 stop api-server

Restart an app:

pm2 restart api-server

Delete an app:

pm2 delete api-server

๐Ÿ”น 5. Watching for Code Changes (Dev Mode)

Automatically restart when files change:

pm2 start app.js --watch

PM2 watches all files in the folder by default.

๐Ÿ”น 6. Running Multiple Instances (Cluster Mode)

Cluster mode automatically uses all CPU cores:

pm2 start app.js -i max

Or specify a number:

pm2 start app.js -i 4

Cluster mode gives:

Load balancing

Fault tolerance

Better performance in production

๐Ÿ”น 7. Viewing Logs

See logs:

pm2 logs

Real-time logs of one app:

pm2 logs api-server

Error logs only:

pm2 logs --err

๐Ÿ”น 8. Monitoring Apps

CPU and memory dashboard:

pm2 monit

JSON dump of status:

pm2 status

๐Ÿ”น 9. Zero-Downtime Deployment

If you modify your code, restart smoothly:

pm2 reload api-server

Reload = no downtime, cluster mode friendly.

๐Ÿ”น 10. Autostart on System Boot

Generate startup script:

pm2 startup

It will give you a command to run next.

Save the current process list:

pm2 save

Now PM2 will restart your apps automatically after a reboot.

๐Ÿ”น 11. Managing Configurations with Ecosystem File

You can create an ecosystem.config.js for multiple apps:

module.exports = {

apps: [

{

name: "api",

script: "./app.js",

instances: "max",

exec_mode: "cluster",

watch: false,

env: {

NODE_ENV: "development",

},

env_production: {

NODE_ENV: "production",

}

}

]

};

Run it:

pm2 start ecosystem.config.js

๐Ÿ”น 12. Common Use Cases

Hosting APIs

Long-running workers

Cron/queue processors

IoT + edge scripts

Multi-core scaling

High availability Node.js apps

Zero-downtime updates

๐ŸŽฏ Summary

PM2 makes running Node.js apps in production easier by providing:

Process monitoring

Auto-restart on crashes

CPU load balancing

Log management

Zero-downtime reloads

Startup scripts

It is one of the most popular and reliable tools for managing Node.js services.

Learn MERN Stack Training in Hyderabad

Read More

Setting up NGINX for a MERN App

Continuous Deployment with Vercel & Heroku

Dockerizing a MERN Stack App

Using GitHub Actions to Deploy MERN Apps

Visit Our Quality Thought Training Institute in Hyderabad

Get Directions 

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive