pm2 process guardian to ensure the stable operation of nodejs

pm2 process guardian to ensure the stable operation of nodejs

Originally wrote a nodeservice, once I found abnormal service hung up, but did not find any good way to wrap the exception chance to see behind pm2and found a nodematching artifact Service

1. What is pm2

  • After pm2 is the guardian of nodejs process running in the background, an abnormal stop could automatically restart,
  • Can also partial to other third-party command-line program, such as command-line php

2. Why pm2

Because pm2it can easily and quickly to help us manage the command-line program and has good logging and monitoring mechanisms

  • Built-in load balancing (using Node cluster cluster module)
  • Background process
  • 0 seconds off Overload, I understand the general idea is to maintain the upgrade when no downtime.
  • Ubuntu and CentOS has a startup script
  • Stop unstable process (to avoid infinite loop)
  • Console Detection
  • Provide HTTP API
  • Remote control and real-time interface API (Nodejs module allows administrators to interact and PM2 process)

3. pm2 installation

Command line installation pm2
npm install -g pm2

4. pm2 of basic commands

  • pm2 startxxx -i 4 # running in the background pm2, starting four app.js
    can pass the 'max' parameter to start
    the number of cores depends on the number of correct process of Cpu
  • pm2 start xxx --name my-api # naming process
  • pm2 list : Display the status of all processes
  • pm2 monit: Monitors all processes
  • pm2 logs : Show all processes log
  • pm2 stop all: Stop all processes
  • pm2 restart all : Restart all processes
  • pm2 reload all : 0 seconds overload shutdown process (for NETWORKED process)
  • pm2 stop xxx: Stop the specified process
  • pxm2 restart xxx: Restart specified process
  • pm2 startup : Init scripts generated to keep the process alive
  • pm2 web : Run robust computer API endpoint
  • pm2 delete xxx: to kill the specified process
  • pm2 delete all : Kill all processes

5. Start script

pm2Can be executed directly jsonstartup script format is an array type of script, a script that can launch multiple services

  • name: Application Name
  • script: Start script path
  • cwd: Start of the path of the application
  • args: Parameters passed to the script
  • interpreter: The specified script interpreter
  • interpreter_args: Parameters passed to interpreter
  • instances: The number of instances of start, only the valid cluster mode, default fork
  • exec_mode: Application startup mode, support fork and cluster model
  • watch: Listening to restart, under circumstances enabled, folder or sub-folder changes apply automatically restart
  • ignore_watch: Ignore listening folders, support for regular expressions
  • max_memory_restart: The maximum amount of memory limit, beyond the automatic restart
  • env: Environmental variables, object type, such as { "NODE_ENV": "production", "ID": "42"}
  • log_date_format: Specify a log date format, such as YYYY-MM-DD HH: mm: ss
  • error_file: Records standard error stream, $ HOME / .pm2 / logs / XXXerr.log), coding errors can be found in the file
  • out_file: Recording standard output stream, $ HOME / .pm2 / logs / XXXout.log), a number of standard applications such as printing output, will cause pm2日志过大
  • min_uptime: Less than an application running time is considered abnormal start
  • max_restarts: The maximum number of abnormal restart, running time that is less than the number of restarts min_uptime
  • autorestart: The default is true, automatically restart in the event of abnormal
  • cron_restart: Crontab time format to restart the application, currently only supports cluster mode
  • force: Default false, if true, could start a script repeat. pm2 not recommended
  • restart_delay: Abnormal restart, the restart delay time

E.g:

[{
  "name": "nodejs 画图服务",
  "args": "",
  "script": "./post.js",
  "exec_interpreter": "/usr/local/bin/node",
  "exec_mode": "fork",
  "max_memory_restart": "400M"
}]
Published 74 original articles · won praise 49 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_34208844/article/details/88696236