Nodejs PM2 Tutorial (reprint)

Source: Internet
Author: User

Http://www.cnblogs.com/laien/p/5826080.html

First, Introduction

PM2 is an application process manager with load balancing, similar to Supervisor,forever.

Second, installation

Linux binaries:https://nodejs.org/dist

    1. CD ONEINSTACK/SRC
    2. wget https://nodejs.org/dist/v4.2.4/node-v4.2.4-linux-x64.tar.gz
    3. Tar xzf node-v4.2.4-linux-x64.tar.gz
    4. CP node-v4.2.4-linux-x64/bin/node/usr/local/bin/
    5. Cp-r node-v4.2.4-linux-x64/lib/node_modules/usr/local/lib/
    6. Ln-s/USR/LOCAL/LIB/NODE_MODULES/NPM/BIN/NPM-CLI.JS/USR/LOCAL/BIN/NPM
    7. NPM install [email protected]-G #安装最新版本pm2模块

PS: If your host is unable to connect to the public network, first find a host that can connect to the public network installed PM2, and then copy to the host you want to install. Copy the following directory:

    1. /usr/local/bin/node
    2. /usr/local/lib/node_modules

To create a related soft connection again

Three, PM2 commonly used commands

Suppose you have now written a app.js file that needs to be started and you can manage it using PM2

1. Start
    1. # PM2 Start App.js
    2. # PM2 Start app.js--name my-api #my-api for PM2 process name
    3. # PM2 start App.js-i 0 #根据CPU核数启动进程个数
    4. # PM2 start app.js--watch #实时监控app. js, PM2 will automatically reload when App.js file is changed
2. View the process
    1. # PM2 List
    2. # PM2 Show 0 or # PM2 info 0 #查看进程详细信息, 0 for PM2 process ID
3. Monitoring
    1. # PM2 Monit
4. Stop
    1. # PM2 Stop All #停止PM2列表中所有的进程
    2. # PM2 Stop 0 #停止PM2列表中进程为0的进程
5. Overloading
    1. # PM2 Reload All #重载PM2列表中所有的进程
    2. # PM2 Reload 0 #重载PM2列表中进程为0的进程
6. Restart
    1. # PM2 Restart all #重启PM2列表中所有的进程
    2. # PM2 Restart 0 #重启PM2列表中进程为0的进程
7. Delete the PM2 process
    1. # PM2 Delete 0 #删除PM2列表中进程为0的进程
    2. # PM2 Delete all #删除PM2列表中所有的进程
8. Log operations
    1. # PM2 logs [--raw] #Display all processes logs in streaming
    2. # PM2 Flush #Empty All log file
    3. # PM2 Reloadlogs #Reload all logs
9. Upgrade PM2
    1. # NPM Install [email protected]-G #安装最新的PM2版本
    2. # PM2 UpdatePM2 #升级pm2
10. See Help for more command parameters
    1. # PM2--help
IV. directory Structure of PM2

The default directory is: the. PM2 directory currently used in the home directory (this directory can be customized, please refer to: Five, custom boot files), the details are as follows:

    1. $HOME/.pm2 #will contain all PM2 related files
    2. $HOME/.pm2/logs #will contain all applications logs
    3. $HOME/.pm2/pids #will contain all applications PIDs
    4. $HOME/.pm2/pm2.log #PM2 Logs
    5. $HOME/.pm2/pm2.pid #PM2 PID
    6. $HOME/.pm2/rpc.sock #Socket file for remote commands
    7. $HOME/.pm2/pub.sock #Socket file for publishable events
    8. $HOME/.pm2/conf.js #PM2 Configuration
V. Customizing startup files

Create a Test.json sample file in the following format:

    1. {
    2. "Apps":
    3. {
    4. "Name": "Test",
    5. "CWD": "/data/wwwroot/nodejs",
    6. "Script": "./test.sh",
    7. "Exec_interpreter": "Bash",
    8. "Min_uptime": "60s",
    9. "Max_restarts": 30,
    10. "Exec_mode": "Cluster_mode",
    11. "Error_file": "./test-err.log",
    12. "Out_file": "./test-out.log",
    13. "Pid_file": "./test.pid"
    14. "Watch": false
    15. }
    16. }

Description

Apps: JSON structure, apps is an array, and each array member is an application that runs in a PM2

Name: Names of applications

CWD: The directory where the application resides

Script: The application's scripting path

exec_interpreter: The script type of the application, the shell used here, the default is Nodejs

min_uptime: Minimum run time, set here is 60s that is, if the application exits within 60s, PM2 will assume that the program exits unexpectedly, triggering the restart Max_restarts setting number

max_restarts: Set the number of times the application unexpectedly exits the restart, by default 15 times (counting from 0)

exec_mode: Application startup mode, which is set to Cluster_mode (cluster), the default is fork

error_file: Error log file for custom application

out_file: Custom Application log files

pid_file: Customizing the application's PID file

Watch: If monitoring mode is enabled, the default is False. If set to True, PM2 is automatically overloaded when the application changes. You can also set up the files you want to monitor.

Detailed parameter list: see annex VIII

Vi. examples

As an example of the above Test.json

    1. # cat >/data/wwwroot/nodejs/test.sh << EOF
    2. #!/bin/bash
    3. While:
    4. Do
    5. echo "Test" >> 1.log
    6. Sleep 5
    7. Done
    8. Eof
    1. # chmod +x test.sh #添加执行权限
    2. # PM2 Start Test.json #启动, such as:
    1. # PM2 list #查看pm2进程, such as:
Vii. remarks

Other parameters can be found on the official website: Http://pm2.keymetrics.io

Viii. Annex

Field Type Example Description
Name String "MyApi" Name your app'll has in PM2
Script String "Bin/app.js" Path of your app
Args List ["--enable-logs", "-N", "15"] Arguments given to your app if it is launched
Node_args List ["--harmony", "--max-stack-size=1024"] Arguments given to node while it is launched
Cwd String "/var/www/app/prod" The directory from which your app would be launched
Exec_mode String "Cluster" "Fork" mode is used by default, "cluster" mode can being configured with instances field
Instances Number 4 Number of instances for your clustered apps, 0 means as much instances as you have CPU cores. A negative value means CPU cores-value (e.g-1 on a 4 cores machine would spawn 3 instances)
Exec_interpreter String "Node" Defaults to "Node". can be "Python", "Ruby", "bash" or whatever interpreter your wish to use. "None" would execute your app as a binary executable
Log_date_format String "Yyyy-mm-dd hh:mm Z" Format in which timestamps'll be displayed in the logs
Error_file String "/var/log/node-app/node-app.stderr.log" Path to the specified error log file. PM2 generates one by default if not specified and can find it by typing pm2 desc <app id>
Out_file String "/var/log/node-app/node-app.stdout.log" Path to the specified output log file. PM2 generates one by default if not specified and can find it by typing pm2 desc <app id>
Pid_file String "Pids/node-geo-api.pid" Path to the specified PID file. PM2 generates one by default if not specified and can find it by typing pm2 desc <app id>
Merge_logs Boolean False Defaults to False. If true, it would merge logs from all instances of the same app into the same file
Cron_restart String "1 0 * * *" A cron pattern to restart your app. Only works in "cluster" mode for now. Soon to being avaible in "fork" mode as well
Watch Boolean True Enables the watch feature, defaults to "false". If true, it'll restart your app everytime a file change was detected on the folder or subfolder of your app.
Ignore_watch List ["[\/\\]\./", "Node_modules"] List of regex to ignore some file or folder names by the Watch feature
Min_uptime Number 1000 Min Uptime of the app to being considered started (i.e. if the app crashes in this time frame, the app is only being restarted The number set in Max_restarts (default), after that it's errored)
Max_restarts Number 10 Number of consecutive unstable restarts (less than 1sec interval or custom time via Min_uptime) before your app is Conside Red errored and stop being
Max_memory_restart String "150M" Your app is restarted by PM2 if it exceeds the amount of memory specified. human-friendly format:it can be ' 10M ', ' 100K ', ' 2G ' and so on ...
Env Object {"Node_env": "Production", "ID": "42"} env variables which'll appear in your app
AutoRestart Boolean False True by default. If False, PM2 would not restart your app if it crashes or ends peacefully
Vizion Boolean False True by default. If False, PM2 'll start Without Vizion features (versioning control Metadatas)
Post_update List ["NPM Install", "Echo launching the app"] A list of commands which would be executed after you perform a pull/upgrade operation from Keymetrics Dashboard
Force Boolean True Defaults to False. If true, you can start the same script several times which are usually not allowed by PM2
Next_gen_js Boolean True Defaults to False. If true, PM2 'll launch your app using embedded BABELJS features which means you can run ES6/ES7 JavaScript code
Restart_delay Number 4000 Time to wait before restarting a crashed app (in milliseconds). Defaults to 0.

Nodejs PM2 Tutorial (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.