Nodejs PM2 Configuration Using Tutorials

Source: Internet
Author: User

PM2 is a very good tool for running managed services on a node. JS-based project. It is based on the command line interface and offers many features: built-in load balancer and so on, let's take a look.

First, Introduction

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

Second, installation

Linux binaries:https://nodejs.org/dist
CD ONEINSTACK/SRC
wget https://nodejs.org/dist/v4.2.4/node-v4.2.4-linux-x64.tar.gz
Tar xzf node-v4.2.4-linux-x64.tar.gz
CP node-v4.2.4-linux-x64/bin/node/usr/local/bin/
Cp-r node-v4.2.4-linux-x64/lib/node_modules/usr/local/lib/
Ln-s/USR/LOCAL/LIB/NODE_MODULES/NPM/BIN/NPM-CLI.JS/USR/LOCAL/BIN/NPM
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:
/usr/local/bin/node
/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

# PM2 Start App.js
# PM2 Start app.js--name my-api #my-api for PM2 process name
# PM2 start App.js-i 0 #根据CPU核数启动进程个数
# PM2 start app.js--watch #实时监控app. js, PM2 will automatically reload when App.js file is changed
2. View the process
# PM2 List
# PM2 Show 0 or # PM2 info 0 #查看进程详细信息, 0 for PM2 process ID
3. Monitoring
# PM2 Monit
4. Stop
# PM2 Stop All #停止PM2列表中所有的进程
# PM2 Stop 0 #停止PM2列表中进程为0的进程
5. Overloading
# PM2 Reload All #重载PM2列表中所有的进程
# PM2 Reload 0 #重载PM2列表中进程为0的进程
6. Restart
# PM2 Restart all #重启PM2列表中所有的进程
# PM2 Restart 0 #重启PM2列表中进程为0的进程
7. Delete the PM2 process
# PM2 Delete 0 #删除PM2列表中进程为0的进程
# PM2 Delete all #删除PM2列表中所有的进程
8. Log operations
# PM2 logs [--raw] #Display all processes logs in streaming
# PM2 Flush #Empty All log file
# PM2 Reloadlogs #Reload all logs
9. Upgrade PM2
# NPM Install [email protected]-G #安装最新的PM2版本
# PM2 UpdatePM2 #升级pm2
10. See Help for more command parameters
# 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:
$HOME/.pm2 #will contain all PM2 related files
$HOME/.pm2/logs #will contain all applications logs
$HOME/.pm2/pids #will contain all applications PIDs
$HOME/.pm2/pm2.log #PM2 Logs
$HOME/.pm2/pm2.pid #PM2 PID
$HOME/.pm2/rpc.sock #Socket file for remote commands
$HOME/.pm2/pub.sock #Socket file for publishable events
$HOME/.pm2/conf.js #PM2 Configuration

V. Customizing startup files

Create a Test.json sample file in the following format:
{
"Apps":
{
"Name": "Test",
"CWD": "/data/wwwroot/nodejs",
"Script": "./test.sh",
"Exec_interpreter": "Bash",
"Min_uptime": "60s",
"Max_restarts": 30,
"Exec_mode": "Cluster_mode",
"Error_file": "./test-err.log",
"Out_file": "./test-out.log",
"Pid_file": "./test.pid"
"Watch": false
}
}

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

# cat >/data/wwwroot/nodejs/test.sh << EOF
#!/bin/bash
While:
Do
echo "Test" >> 1.log
Sleep 5
Done
Eof
# chmod +x test.sh #添加执行权限
# PM2 Start Test.json #启动, such as:

# PM2 list #查看pm2进程, such as:

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

=============================================================================================================== =====

Installation: NPM install-g PM2

Startup program: PM2 start <app_name|id|all> list process: PM2 list Exit Program: PM2 Stop <app_name|id|all> Restart application: PM2 Restart program information: PM2 Describe Id|all monitoring: PM2 monit Real-time centralized log processing: PM2 LOGSAPI:PM2 Web (port: 9615)

Nodejs PM2 Configuration Using Tutorials

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.