pm2 process guardian to ensure the stable operation of nodejs
Originally wrote a
node
service, once I found abnormal service hung up, but did not find any good way to wrap the exception chance to see behindpm2
and found anode
matching 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 pm2
it 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 start
xxx -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 Cpupm2 start
xxx --name my-api # naming processpm2 list
: Display the status of all processespm2 monit
: Monitors all processespm2 logs
: Show all processes logpm2 stop all
: Stop all processespm2 restart all
: Restart all processespm2 reload all
: 0 seconds overload shutdown process (for NETWORKED process)pm2 stop
xxx: Stop the specified processpxm2 restart
xxx: Restart specified processpm2 startup
: Init scripts generated to keep the process alivepm2 web
: Run robust computer API endpointpm2 delete
xxx: to kill the specified processpm2 delete all
: Kill all processes
5. Start script
pm2
Can be executed directly json
startup script format is an array type of script, a script that can launch multiple services
name
: Application Namescript
: Start script pathcwd
: Start of the path of the applicationargs
: Parameters passed to the scriptinterpreter
: The specified script interpreterinterpreter_args
: Parameters passed to interpreterinstances
: The number of instances of start, only the valid cluster mode, default forkexec_mode
: Application startup mode, support fork and cluster modelwatch
: Listening to restart, under circumstances enabled, folder or sub-folder changes apply automatically restartignore_watch
: Ignore listening folders, support for regular expressionsmax_memory_restart
: The maximum amount of memory limit, beyond the automatic restartenv
: 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: sserror_file
: Records standard error stream, $ HOME / .pm2 / logs / XXXerr.log), coding errors can be found in the fileout_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 startmax_restarts
: The maximum number of abnormal restart, running time that is less than the number of restarts min_uptimeautorestart
: The default is true, automatically restart in the event of abnormalcron_restart
: Crontab time format to restart the application, currently only supports cluster modeforce
: Default false, if true, could start a script repeat. pm2 not recommendedrestart_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"
}]