Home  >  Article  >  Web Front-end  >  How to use pm2 in Node.js (graphic tutorial)

How to use pm2 in Node.js (graphic tutorial)

亚连
亚连Original
2018-05-18 14:52:122013browse

pm2 is a process manager for Node applications with load balancing function. You can put your standalone code to utilize all CPUs on all servers and ensure that the process is always alive with 0 seconds of reload.

The main features of pm2:

1. Built-in load balancing (using Node cluster cluster module)
2. Background running
3. 0-second shutdown and reload
4. Startup scripts with Ubuntu and CentOS
5. Stop unstable processes (avoid infinite loops)
6. Console detection
7. Provide HTTP API
8, remote control and real-time interface API (Nodejs module, allowing interaction with PM2 process manager)

PM2 installation:

npm install -g pm21

Usage of pm2:

pm2 start app.js -i 4   // 后台运行pm2,启动4个app.js 
                        // 也可以把'max' 参数传递给 start
                        // 正确的进程数目依赖于Cpu的核心数目pm2 start app.js --name my-api // 命名进程pm2 list               // 显示所有进程状态pm2 monit              // 监视所有进程pm2 logs               //  显示所有进程日志pm2 stop all           // 停止所有进程pm2 restart all        // 重启所有进程pm2 reload all         // 0秒停机重载进程 (用于 NETWORKED 进程)pm2 stop 0             // 停止指定的进程pm2 restart 0          // 重启指定的进程pm2 startup            // 产生 init 脚本 保持进程活着pm2 web                // 运行健壮的 computer API endpoint pm2 delete 0           // 杀死指定的进程pm2 delete all         // 杀死全部进程12345678910111213141516

Different ways of running the process of pm2:

pm2 start app.js -i max  // 根据有效CPU数目启动最大进程数目pm2 start app.js -i 3      // 启动3个进程pm2 start app.js -x        //用fork模式启动 app.js 而不是使用 clusterpm2 start app.js -x -- -a 23   // 用fork模式启动 app.js 并且传递参数 (-a 23)pm2 start app.js --name serverone  // 启动一个进程并把它命名为 serveronepm2 stop serverone       // 停止 serverone 进程pm2 start app.json        // 启动进程, 在 app.json里设置选项pm2 start app.js -i max -- -a 23                   //在--之后给 app.js 传递参数pm2 start app.js -i max -e err.log -o out.log  // 启动并生成一个配置文件// 也可以执行用其他语言编写的app  ( fork 模式):pm2 start my-bash-script.sh    -x --interpreter bash
pm2 start my-python-script.py -x --interpreter python12345678910111213

Common command usage:

pm2 list

Lists all process information managed by pm2, and also displays how many times a process will be started due to unhandled exceptions.

pm2 monit

Monitor the CPU and memory usage of each node process

The above is me I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

How to submit a request using JS using POST method (detailed answer combined with code)

jsExecute an instance of a function after the code delays for a certain period of time

Avalonjs Implement a simple shopping cart function

The above is the detailed content of How to use pm2 in Node.js (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn