Process Management with PM2
Process Management with PM2
While node app.js is fine for development, production environments require a process manager to keep the application running continuously, automatically restart it upon crashes, and manage logs. PM2 is the industry standard for this.
1. Installing PM2
Install PM2 globally so the command is available anywhere on your system:
npm install -g pm22. Basic Commands
Starting an application
pm2 start app.js --name "my-node-api"Listing running apps
pm2 listpm2 statusRestarting / Stopping / Deleting
pm2 restart my-node-apipm2 stop my-node-apipm2 delete my-node-apiViewing Logs
To see real-time logs for your application:
pm2 logs my-node-api3. Cluster Mode
Node.js is single-threaded. PM2 can automatically scale your application across all available CPU cores using Cluster Mode, maximizing performance.
pm2 start app.js -i max(The -i max flag tells PM2 to start as many instances as there are CPU cores).
4. PM2 Ecosystem File
Instead of passing long command-line arguments, you can define a configuration file.
Generate a template:
pm2 initExample ecosystem.config.js:
module.exports = { apps : [{ name : "my-api", script : "./app.js", instances: "max", env_production: { NODE_ENV: "production", PORT: 8080 } }]}Run with: pm2 start ecosystem.config.js --env production
5. Startup Script
To ensure PM2 automatically starts your Node apps when the server reboots:
pm2 startuppm2 save