SoFunction
Updated on 2025-03-10

Briefly describe the commonly used command collection and configuration file description of pm2

Commonly used command collection

pm2 start             # Start the application
pm2 start -i 4     # cluster mode starts 4 application instances # 4 applications will automatically load balance
pm2 start --name="api" # Start the application and name it "api"
pm2 start --watch    # Automatically restart the application when the file changes
pm2 start          # Start bash script
pm2 list                          # List All applications launched by PM2
pm2 monit                                                                                                                            �
pm2 show [app-name]     # Show all information about the application
pm2 logs
pm2 logs [app-name]            # Display the log of the specified application
pm2 flush
pm2 stop all
pm2 stop 0                                                                                                                            �
pm2 restart all          # Restart all applications
pm2 reload all             # Restart all applications under cluster mode
pm2 gracefulReload all    # Graceful reload all apps in cluster mode
pm2 delete all                # Close and delete all applications
pm2 delete 0                                                            �
pm2 scale api 10                  # Extend the application named api to 10 instances
pm2 reset [app-name]      # Reset the number of restarts
pm2 startup                 # Create a boot command
pm2 save                                                                                                                             �
pm2 resurrect                  # Reload the saved application list
pm2 update                # Save processes, kill PM2 and restore processes
pm2 generate               # Generate a sample json configuration file
pm2 start --node-args="--max-old-space-size=1024"

Detailed introduction to configuration files

{
  "apps": {
    "name": "wuwu",               // Project name    "script": "./bin/www",           // Execute the file    "cwd": "./",                // Root directory    "args": "",                 // Parameters passed to the script    "interpreter": "",             // Specified script interpreter    "interpreter_args": "",           // Parameters passed to the interpreter    "watch": true,               // Whether to listen for file changes and restart    "ignore_watch": [              // Files that do not need to be monitored      "node_modules",
      "logs"
    ],
    "exec_mode": "cluster_mode",        // Application startup mode, support fork and cluster modes    "instances": 4,               // The number of application startup instances is only valid in cluster mode. The default is fork; or max    "max_memory_restart": 8,          // Maximum memory limit, automatic restart exceeds    "error_file": "./logs/",     // Error log file    "out_file": "./logs/",      // Normal log files    "merge_logs": true,             // Set append log instead of creating new log    "log_date_format": "YYYY-MM-DD HH:mm:ss",  // Specify the time format of the log file    "min_uptime": "60s",            // The application runs less than the time it is considered an abnormal startup    "max_restarts": 30,             // The maximum number of abnormal restarts is less than the number of restarts when the min_uptime runs;    "autorestart": true,            // Default is true, automatically restart if an exception occurs    "cron_restart": "",             // Restart the application in the crontab time format, currently only supports cluster mode;    "restart_delay": "60s"           // In the case of abnormal restart, delayed restart time    "env": {
      "NODE_ENV": "production",        // Environment parameter, currently specified as production environment .NODE_ENV      "REMOTE_ADDR": "Fall in love with loud"        // .REMOTE_ADDR
    },
    "env_dev": {
      "NODE_ENV": "development",       // Environment parameters, currently specified as development environment pm2 start --env_dev      "REMOTE_ADDR": ""
    },
    "env_test": {                // Environment parameters, currently specified as the test environment pm2 start --env_test      "NODE_ENV": "test",
      "REMOTE_ADDR": ""
    }
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.