PM2 Quick Start

PM2 Tutorial

@(Node)[Load Balance|Process Manager]

[TOC]


Introduction to PM2

PM2 is a process manager for Node applications with load balancing.
PM2 is perfect when you want your standalone code to utilize all CPUs on all servers, and keep processes alive forever, with 0 seconds of reload . It is very suitable for IaaS structure, but don't use it for PaaS solution (solution for Paas will be developed later).

Remark

   · SaaS、PaaS和IaaS是云服务模式。
   · SaaS 软件即服务,例如Google的 Gmail 邮箱服务.面向应用型用户.
   · PaaS 平台即服务.例如Google的GAE,面向开发型用户
   · IaaS  基础架构即服务,例如亚马逊的AWS,IaaS对于不知道新推出的应用程序/网站会有多成功的创业公司来说非常有用

Main features:

  • Built-in load balancing (using the Node cluster cluster module)
  • Background process
  • 0 seconds downtime and reloading, I understand that it means that no downtime is required during maintenance and upgrades.
  • Startup scripts with Ubuntu and CentOS
  • Stop unstable processes (avoid infinite loops)
  • console detection
  • Provide HTTP API
  • Remote control and real-time interface API (Nodejs module, allows interaction with PM2 process manager)
  • Tested Nodejs v0.11 v0.10 v0.8 version, compatible with CoffeeScript, based on Linux and MacOS.

Install

    npm install -g pm2

usage

$ 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         # 杀死全部进程

Different ways to run a process:

$ 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 seconds downtime and reload:

This feature allows you to reload code without losing the request connection.

Notice:

  • Can only be used for web applications
  • Running on Node 0.11.x version
  • Runs in cluster mode (default mode)
$ pm2 reload all

CoffeeScript:

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

PM2 Product Level Services

Just test on your server

$ git clone https://github.com/Unitech/pm2.git
$ cd pm2
$ npm install # 或者 npm install --dev ,如果devDependencies 没有安装
$ npm test

pm2 list

Lists all process information managed by pm2, and also shows how many times a process will be started because of unhandled exceptions.

pm2 list 

Alt text

pm2 monit

Monitor the CPU and memory usage of each node process.

Alt text

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324894386&siteId=291194637