Pm2 is a process manager for Node applications with load balancing function. You can take your individual code and use all cpus on all servers and keep the process alive forever, 0 second reload.

Main features of PM2

1, built-in load balancing (using Node Cluster cluster module) 2, background running 3, 0 second downtime overload 4, Ubuntu and CentOS startup scripts 5, stop unstable processes (avoid infinite loop) 6, console detection 7, HTTP API 8. Remote control and real-time API (Nodejs module, allowing interaction with PM2 process manager)

The installation of pm2

npm install -g pm2
Copy the code

The use of the pm2

Pm2 start app.js -i 4 pm2 start app.js -i 4 pm2 start app.js'max'Pm2 start app.js --name my-api // Name processes pm2 list // Display the status of all processes pm2 monit // Monitor all processes Pm2 Pm2 stop all // Stop all processes pm2 restart all // Restart all processes pm2 reload all // 0 seconds Stop Overloaded processes (for NETWORKED processes) pm2 stop 0 // Stop the specified process pm2 restart 0 // Restart the specified process pm2 startup // Generate init script to keep the process alive pm2 web // Run robust computer API endpoint pm2 delete 0 // Kill the specified process pm2 delete all // Kill all processes 12345678910111213141516Copy the code

The different ways pM2 runs processes

Pm2 start app.js -i Max // Maximum number of processes to be started based on the number of available cpus pm2 start app.js -i 3 // Start three processes pm2 start app.js -x // Start app.js in fork mode Instead of using cluster pm2 start app.js-x ---a23 // Start app.js with fork mode and pass parameters (-a23) pm2 start app.js --name serverOne // Start a process and name it serverone pm2 stop Serverone // Stop serverOne process pm2 start app.json // Start the process with the option pm2 start app.js -i Max -- in app.json-a23 // Pass the pm2 start app.js -i Max parameter to app.js after ---eErr.log -o out.log // Start and generate a configuration file // Can also execute app written in other languages (fork mode): pm2 start my-bash-script.sh -x --interpreter bash pm2 start my-python-script.py -x --interpreter python12345678910111213Copy the code

pm2 list

Lists all processes managed by PM2 and also shows how many times a process will be started due to unhandled exceptions.

pm2 monit

Monitor CPU and memory usage for each Node process.

pm2 logs

Real-time centralized log processing.

Fast recovery

Now that things are going well and your process is humming along, you need to do a hard restart. Now? Yes, first,dump:

$ pm2 dump
Copy the code

You can then recover it from the file:

$ pm2 kill// Let's assume that a PM2 has stopped $PM2 resurect // all my processes come back to full healthCopy the code

Summary of Common Commands

Pm2 logs Displays logs of all processes pm2 stop All Stop all processes pm2 restart All Restart all processes pm2 reload All 0 seconds Stop overloaded processes (for NETWORKED processes) pm2 stop 0 Pm2 startup Generate the init script to keep the process alive. Pm2 Web Run a robust computer API endpoint (http://localhost:9615). Pm2 delete 0 Kills the specified process. Pm2 delete all Kills all processesCopy the code

Different ways to run processes

Js -i Max Indicates the maximum number of processes to be started based on the number of available cpus. Pm2 start app.js -i 3 Starts three processes. Pm2 start app.js -x Starts app.js in fork mode instead of using cluster pm2 start app.js -x ---a23 Start app.js with fork and pass parameters (-a23) pm2 start app.js --name serverOne Start a process and name it serverone pm2 stop ServerOne Pm2 start app.json In app.json set the option pm2 start app.js -i Max ---a23 Pass the pm2 start app.js -i Max parameter to app.js after ---eErr.log -o out.log starts and generates a configuration fileCopy the code

Configure the PM2 startup file

Add a processs. json to the project root directory:

{
  "apps": [{"name": "mywork"."cwd": "/srv/node-app/current"."script": "bin/www"."log_date_format": "YYYY-MM-DD HH:mm Z"."error_file": "/var/log/node-app/node-app.stderr.log"."out_file": "log/node-app.stdout.log"."pid_file": "pids/node-geo-api.pid"."instances": 6."min_uptime": "200s"."max_restarts": 10."max_memory_restart": "1M"."cron_restart": "1 0 * * *"."watch": false."merge_logs": true."exec_interpreter": "node"."exec_mode": "fork"."autorestart": false."vizion": false}}]Copy the code

Description:

  • Apps: JSON structure. Apps is an array, and each array member corresponds to an application running in PM2
  • Name: indicates the application name
  • CWD: the directory where the application resides
  • Script: script path of the application
  • log_date_format:
  • Error_file: error log file of the custom application
  • Out_file: custom application log file
  • Pid_file: Pid file for custom application
  • instances:
  • Min_uptime: the amount of restarts for max_restarts is triggered if the application exits within 60 seconds
  • Restarts for max_restarts: Restarts for the application for an unexpected exit, 15 times by default (counting from 0)
  • Cron_restart: starts periodically to resolve problems that can be resolved after the restart
  • Watch: Whether to enable monitoring mode. The default value is false. If set to true, pM2 is automatically reloaded when the application changes. This is also where you can set the files you want to monitor.
  • merge_logs:
  • Exec_interpreter: The script type for the application. The shell used here is nodeJS by default
  • Exec_mode: Application startup mode, which is set to Cluster_mode (cluster) and fork by default
  • Autorestart: Enables/disables automatic restart when an application crashes or exits
  • Vizion: Enable/disable Vizion features (version control)

Can be achieved bypm2 start processes.jsonTo start. You can also write the command in package.json as follows:

 "scripts": {
    "dev": "NODE_ENV=development nodemon src/server.js & NODE_ENV=development nodemon src/server/action-server.js & tools/redis/socket.js",
    "dev_read_redis": "NODE_ENV=development nodemon src/app.js",
    "start": "NODE_ENV=production nodemon src/app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
Copy the code

Start the

npm run start
Copy the code

Summary from:

www.cnblogs.com/zhoujie/p/n…

Blog.csdn.net/starter____…