Home > Web Front-end > JS Tutorial > body text

Use PM2 to manage nodejs process sharing

小云云
Release: 2018-01-03 13:44:10
Original
1671 people have browsed it

pm2 is a process manager for Node applications with load balancing function. This article mainly introduces the detailed explanation of using PM2 to manage nodejs processes. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

PM2 is perfect when you want your standalone code to utilize all CPUs on all servers and ensure that the process is always alive with 0 second reloads.

It is very suitable for IaaS structure, but do not use it for PaaS solutions (Paas solutions will be developed later).

Compared with using node index.js, advantages:

1 One command window can manage multiple node server processes. The node command requires multiple processes to open multiple windows.

2 Close the command window and the node process will still run. After the node command is run and the window is closed, the process is also closed.

First install PM2

npm install -g pm2
Copy after login

Second, use pm2 to start index.js

pm2 start index.js --name tank

--name tank is to give this process a name

Three other pm2 commands

安装
npm install -g pm2

用法
$ npm install pm2 -g   # 命令行安装 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 (http://localhost:9615)
$ pm2 delete 0      # 杀死指定的进程
$ pm2 delete all     # 杀死全部进程

运行进程的不同方式:
$ pm2 start app.js -i max # 根据有效CPU数目启动最大进程数目
$ pm2 start app.js -i 3   # 启动3个进程
$ pm2 start app.js -x    #用fork模式启动 app.js 而不是使用 cluster
$ pm2 start app.js -x -- -a 23  # 用fork模式启动 app.js 并且传递参数 (-a 23)
$ pm2 start app.js --name serverone # 启动一个进程并把它命名为 serverone
$ pm2 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 python

0秒停机重载:
这项功能允许你重新载入代码而不用失去请求连接。
注意:
仅能用于web应用
运行于Node 0.11.x版本
运行于 cluster 模式(默认模式)
$ pm2 reload all

CoffeeScript:
$ pm2 start my_app.coffee #这就是全部

PM2准备好为产品级服务了吗?
只需在你的服务器上测试
$ git clone https://github.com/Unitech/pm2.git
$ cd pm2
$ npm install # 或者 npm install --dev ,如果devDependencies 没有安装
$ npm test
Copy after login

Related recommendations:

How to use PM2 to make Node.js clustering easier

PM2.5 Data Collection_PHP Tutorial

PHP FastCGI Architecture of process manager PHP-FPM

The above is the detailed content of Use PM2 to manage nodejs process sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template