Skip to content

Instantly share code, notes, and snippets.

@mizanmahi
Last active November 13, 2024 19:35
Show Gist options
  • Save mizanmahi/8249d33dde53978f6342129abaa1d4ad to your computer and use it in GitHub Desktop.
Save mizanmahi/8249d33dde53978f6342129abaa1d4ad to your computer and use it in GitHub Desktop.
PM2 Useful Commands

PM2 commands for managing and monitoring Node.js applications:

1. Starting and Stopping Applications

  • Start an application:

    pm2 start app.js

    Starts a Node.js app (app.js), creating a managed process.

  • Start with a specific name:

    pm2 start app.js --name "myApp"
  • Stop an application:

    pm2 stop <app_name or id>
  • Restart an application:

    pm2 restart <app_name or id>
  • Delete an application:

    pm2 delete <app_name or id>

2. Application Management

  • Seeing all running applications:

    pm2 status
  • List all running applications:

    pm2 list
  • View application logs:

    pm2 logs
  • View logs for a specific app:

    pm2 logs <app_name or id>
  • Display details of a specific app:

    pm2 show <app_name or id>
  • Monitor all running apps in real-time:

    pm2 monit

3. Process Scaling

  • Scale an application:
    pm2 scale <app_name or id> <number>
    This command allows you to increase or decrease the number of instances.

4. Environment Variables and JSON Config Files

  • Start with environment variables:

    pm2 start app.js --env production
  • Start an application with a JSON config file:

    pm2 start ecosystem.config.js

5. Saving and Loading Processes

  • Save the current process list:

    pm2 save

    This will allow PM2 to resurrect the saved processes on server reboot.

  • Reload saved process list on startup:

    pm2 resurrect

6. Automatic Restart on Code Changes

  • Enable auto-restart on file changes:

    pm2 start app.js --watch
  • Specify files or folders to watch:

    pm2 start app.js --watch --ignore-watch="node_modules"

7. Startup Script for Reboot Persistence

  • Generate a startup script:
    pm2 startup
    This command generates the command to make PM2 start on boot. Run the output command to enable it.

8. Zero-Downtime Reload

  • Reload all applications with zero downtime:
    pm2 reload all
    This command is especially useful for reloading apps without downtime (great for production).

9. Updating PM2

  • Update PM2 to the latest version:

    pm2 update
  • Reload PM2’s daemon process:

    pm2 updatePM2

10. Miscellaneous Commands

  • Get system information (CPU, RAM usage):

    pm2 info <app_name or id>
  • Clear logs for all applications:

    pm2 flush
  • Kill the PM2 daemon (stops all applications and the PM2 process):

    pm2 kill

Basic JSON configuration file (ecosystem.config.js) for managing two applications using PM2. This file includes environment variables, watch mode, and other configurations. One application is a Node.js (Express) app, and the other is a Python app.

ecosystem.config.js File

module.exports = {
  apps: [
    {
      name: "expressApp",                   // Name of the first app
      script: "app.js",                     // Entry file for the Node.js app
      watch: true,                          // Watch for file changes
      env: {                                // Environment variables for development
        NODE_ENV: "development",
        PORT: 3000,
        DB_URI: "mongodb://localhost:27017/devdb"
      },
      env_production: {                     // Environment variables for production
        NODE_ENV: "production",
        PORT: 8080,
        DB_URI: "mongodb://localhost:27017/proddb"
      },
      ignore_watch: ["node_modules"],       // Ignore changes in the node_modules folder
      instances: 2,                         // Run 2 instances (for load balancing)
      exec_mode: "cluster"                  // Use cluster mode for zero-downtime reloads
    },
    {
      name: "pythonApp",                    // Name of the second app
      script: "python",                     // Command to run Python
      args: "app.py",                       // Script file for the Python app
      interpreter: "python3",               // Specify Python 3 interpreter
      watch: ["./"],                        // Watch for changes in the root directory
      env: {                                // Environment variables for development
        ENV_MODE: "development",
        API_KEY: "dev_api_key"
      },
      env_production: {                     // Environment variables for production
        ENV_MODE: "production",
        API_KEY: "prod_api_key"
      }
    }
  ]
};

Explanation of the Configurations

  • name: Name of the application, which you can use with PM2 commands.
  • script: Specifies the script or command to start the application (app.js for the Express app, app.py for the Python app).
  • watch: Enables PM2's file-watching feature to restart the app if files change.
  • ignore_watch: Prevents PM2 from restarting on changes in specified folders (like node_modules).
  • instances: Sets the number of instances (useful for load balancing).
  • exec_mode: When set to "cluster", PM2 will run the app in cluster mode, allowing zero-downtime restarts.
  • env and env_production: Defines environment variables for development and production modes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment