PM2 For NodeJS Auto Run and Watch

ยท

3 min read

PM2 (Process Manager 2) is a popular production process manager for Node.js applications. It helps you manage and keep your applications online 24/7, providing a number of useful features for managing application processes, monitoring performance, and ensuring stability. Here's a comprehensive overview of PM2:

Key Features of PM2

  1. Process Management: PM2 allows you to start, stop, restart, and manage multiple applications or microservices easily.

  2. Load Balancing: With PM2, you can take advantage of clustering to maximize your application's performance by distributing the load across multiple CPU cores.

  3. Monitoring: PM2 provides built-in monitoring tools to keep track of application performance, memory usage, and CPU usage in real-time.

  4. Log Management: PM2 handles log management by aggregating and rotating logs for all managed applications. It also supports custom log paths and integrates with log management systems.

  5. Startup Scripts: PM2 can generate startup scripts to ensure your applications start automatically when the server reboots.

  6. Deployment: PM2 includes deployment features to automate the deployment process of your applications.

  7. Configuration Management: PM2 supports JSON and YAML configuration files to define application settings and environment variables.

  8. Graceful Reload: PM2 provides zero-downtime reloads by allowing you to reload your applications without downtime.

  9. Integrated Watchdog: Automatically restarts your applications if they crash or become unresponsive.

Installation

You can install PM2 globally using npm:

npm install -g pm2

Basic Commands

  1. Start an application:

     pm2 start app.js
    
  2. List all processes:

     pm2 list
    
  3. Stop a process:

     pm2 stop app
    
  4. Restart a process:

     pm2 restart app
    
  5. Delete a process:

     pm2 delete app
    
  6. Show logs:

     pm2 logs
    

Process Monitoring

You can monitor your applications in real-time:

pm2 monit

This command opens an interactive interface showing CPU and memory usage for each process.

Cluster Mode

PM2 allows you to run your application in cluster mode to take advantage of multi-core systems:

pm2 start app.js -i max

The -i max option tells PM2 to automatically detect the number of available CPUs and run one instance per CPU core.

Configuration Files

PM2 can be configured using JSON or YAML files. Here's an example of a JSON configuration file (ecosystem.config.json):

{
  "apps": [
    {
      "name": "my-app",
      "script": "./app.js",
      "instances": "max",
      "exec_mode": "cluster",
      "env": {
        "NODE_ENV": "development"
      },
      "env_production": {
        "NODE_ENV": "production"
      }
    }
  ]
}

You can start the application using the configuration file:

pm2 start ecosystem.config.json

Log Management

PM2 handles logs for all processes. You can view logs with:

pm2 logs

To specify custom log files:

pm2 start app.js --log-date-format="YYYY-MM-DD HH:mm Z" -o out.log -e err.log

Startup Script

Generate a startup script to ensure PM2 restarts your applications on server reboot:

pm2 startup
pm2 save

The pm2 startup command generates a startup script, and pm2 save saves the current process list.

Deployment

PM2 simplifies deployment with built-in support for deploying applications:

Create a deployment configuration file (ecosystem.config.js):

module.exports = {
  apps: [{
    name: "my-app",
    script: "./app.js"
  }],
  deploy: {
    production: {
      user: "node",
      host: "server.com",
      ref: "origin/main",
      repo: "git@repository.git",
      path: "/var/www/my-app",
      "post-deploy": "npm install && pm2 reload ecosystem.config.js --env production"
    }
  }
}

Deploy your application:

pm2 deploy ecosystem.config.js production

Conclusion

PM2 is a powerful tool for managing Node.js applications in production. It simplifies process management, load balancing, monitoring, and deployment, helping you keep your applications stable and performant. With PM2, you can ensure your applications stay online 24/7 and handle traffic efficiently.

ย