1. Introduction
pm2 is an application process manager with load balancing function, similar to Supervisor and forever. For detailed parameters, see the official website: http://pm2.keymetrics.io
2. Installation
Linux Binaries download address: 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 pm2@latest -g #Install the latest version of the pm2 module
PS: If your host cannot connect to the public network, first find a host that can connect to the public network and install the above method to install pm2, then copy it to the host you want to install, and copy the following directory:
/usr/local/bin/node /usr/local/lib/node_modules
Then create the relevant soft link
3. Common commands of PM2
Assuming that you have now written an app.js file that needs to be started, you can use pm2 to manage it
1. Start
# pm2 start app.js # pm2 start app.js --name my-api #my-api is the PM2 process name # pm2 start app.js -i 0 #Start the number of processes according to the number of CPU cores # pm2 start app.js --watch #Start by monitoring app.js in real time, when the app.js file changes, pm2 will automatically reload
2. View progress
# pm2 list # pm2 show 0 or # pm2 info 0 #View process details, 0 is the PM2 process id
3. Monitoring
# pm2 monit
4. stop
# pm2 stop all # Stop all processes in the PM2 list # pm2 stop 0 # Stop the process whose process is 0 in the PM2 list
5. Overloading
# pm2 reload all #Reload all processes in the PM2 list # pm2 reload 0 #Reload the process whose process is 0 in the PM2 list
6. Reboot
# pm2 restart all #Restart all processes in the PM2 list # pm2 restart 0 #Restart the process whose process is 0 in the PM2 list
7. Delete the PM2 process
# pm2 delete 0 #Delete the process whose process is 0 in the PM2 list # pm2 delete all #Delete all processes in the PM2 list
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 pm2@lastest -g #Install the latest PM2 version # pm2 updatePM2 #upgrade pm2
10. For more command parameters, please see the help
# pm2 --help
Fourth, PM2 directory structure
The default directory is: the .pm2 directory under the current home directory (this directory can be customized, please refer to: 5. Customize the startup file), 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
5. Custom startup file
Create a sample test.json 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 } }
Parameter Description:
apps: json structure, apps is an array, each array member corresponds to an application running in pm2 name: the name of the application cwd: the directory where the application is located script: the script path of the application exec_interpreter: The script type of the application, the shell used here, the default is nodejs min_uptime: the minimum running time, the setting here is 60s, that is, if the application exits within 60s, pm2 will consider that the program exits abnormally, and trigger a restart at this time. The max_restarts setting number max_restarts: Set the number of times the application exits and restarts abnormally, the default is 15 times (counting from 0) exec_mode: application startup mode, here is cluster_mode (cluster), the default is fork error_file: Custom application's error log file out_file: custom application log file pid_file: the pid file of the custom application watch: Whether to enable monitoring mode, the default is false. If set to true, pm2 will automatically reload when the application changes. Here you can also set the files you want to monitor.
6. Example display
Take the above test.json as an example
# cat > /data/wwwroot/nodejs/test.sh <> 1.log sleep 5 done EOF
Then execute the following command:
# chmod +x test.sh #Add execute permission # pm2 start test.json #Start, as shown below: # pm2 list #View the pm2 process, as shown below: