Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and use PM2 of node

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to install and use node PM2. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

PM2 is a node process management tool, which can be used to simplify many tedious tasks of node application management, such as performance monitoring, automatic restart, load balancing, etc., and it is very easy to use.

Install cd oneinstack/srcwget https://nodejs.org/dist/v4.2.4/node-v4.2.4-linux-x64.tar.gztar xzf node-v4.2.4-linux-x64.tar.gzcp 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/npmnpm install pm2@latest-g # install the latest version of the pm2 module

PS: if your CVM cannot connect to the public network, first find the method to install pm2 from the CVM that can connect to the public network, and then copy it to the CVM you want to install, and copy it to the directory as follows:

/ usr/local/bin/node/usr/local/lib/node_modules

Then create the relevant soft connection.

PM2 common commands

Suppose you have already written an app.js file and need to start it. 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 # launched according to the number of CPU cores # pm2 start app.js-- watch # real-time monitoring app.js. When the app.js file changes, pm2 will automatically reload

two。 View the process

# pm2 list# pm2 show 0 or # pm2 info 0 # to view process details, 0 is PM2 process id

3. Monitor and control

# pm2 monit

4. Stop it

# pm2 stop all # stop all processes in the PM2 list # pm2 stop 0 # stop the process in the PM2 list with process 0

5. Heavy load

# pm2 reload all # reload all processes in the PM2 list # pm2 reload 0 # reload the process with process 0 in the PM2 list

6. Restart

# pm2 restart all # restart all processes in the PM2 list # pm2 restart 0 # restart the process with process 0 in the PM2 list

7. Delete the PM2 process

# pm2 delete 0 # Delete processes with process 0 in PM2 list # pm2 delete all # Delete all processes in PM2 list

8. Log operation

# 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, see help.

# pm2-helpPM2 directory structure

The default directory is .pm2 directory under the home directory currently used (this directory can be customized, please refer to: 5. Custom startup 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 Custom Startup File

Create a sample file of test.json 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, and each array member corresponds to an application name running in pm2: application name cwd: application directory script: application script path exec_interpreter: application script type. Shell is used here. The default is nodejsmin_uptime: minimum run time. The setting here is 60s, that is, if the application exits within 60s, pm2 will think that the application exits abnormally. Number of max_restarts settings for triggering restart max_restarts: set the number of times an application exits abnormally and restart. Default is 15 times (counted from 0) exec_mode: application startup mode, where cluster_mode (cluster) is set The default is forkerror_file: custom application error log file out_file: custom application log file pid_file: custom application pid file watch: whether monitoring mode is enabled, the default is false. If set to true, pm2 will be reloaded automatically when the application changes. You can also set up the files you want to monitor here. Example display

Take the above test.json as an example

# cat > / data/wwwroot/nodejs/test.sh 1.log sleep 5doneEOF

Then execute the following command:

# chmod + x test.sh # add execution permission # pm2 start test.json # start, as shown below: # pm2 list # View the pm2 process, as shown below: the above is all the content of the article "how to install and use PM2 for node". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report