Using the PM2 Daemon nodejs command-line program

Source: Internet
Author: User

Introduced

PM2 is a module of the Nodejs application process manager with load balancing, similar to supervisor,forever for process management.

First, Installation:

<PRE>NPM Install pm2-g</pre>

Second, start:

<PRE>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 automatically reload</pre> when the App.js file is changed

Third, review the process:

<PRE>PM2 List
PM2 Show 0 or # PM2 info 0 #查看进程详细信息, 0 for PM2 process id</pre>

Iv. Monitoring:

<PRE>PM2 monit</pre>

Five, stop:

<PRE>PM2 Stop All #停止PM2列表中所有的进程
PM2 Stop 0 #停止PM2列表中进程为0的进程 </pre>

Six, Heavy:

<PRE>PM2 Reload All #重载PM2列表中所有的进程
PM2 Reload 0 #重载PM2列表中进程为0的进程 </pre>

Seven, restart:

<PRE>PM2 Restart all #重启PM2列表中所有的进程
PM2 Restart 0 #重启PM2列表中进程为0的进程 </pre>

Viii. Delete the PM2 process:

<PRE>PM2 Delete 0 #删除PM2列表中进程为0的进程
PM2 Delete all #删除PM2列表中所有的进程 </pre>

Nine, log operation:

<PRE>PM2 logs [--raw] #Display all processes logs in streaming
PM2 Flush #Empty All log file
PM2 reloadlogs #Reload All logs</pre>

Ten, upgrade PM2:

<PRE>NPM install [email protected]-G #安装最新的PM2版本
PM2 updatePM2 #升级pm2 </pre>

For more command parameters, see Help:

<PRE>PM2--help</pre>

12. PM2 Directory Structure:

The default directory is the. PM2 directory currently used in the home directory (this directory can be customized, please refer to: 13, custom boot file), the details are as follows:
<pre> $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</pre>

13. Custom Boot file:

1. Create a Test.json sample file in the following format:
<pre>{
"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
}
}</pre>
2. Parameter Description:
<pre>
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.
</pre>

Using the PM2 Daemon nodejs command-line program

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.