管理应用状态

PM2 is a process manager. It manages your applications states, so you can start, stop, restart and delete processes.

启动:

$ pm2 start app.js --name "my-api"
$ pm2 start web.js --name "web-interface"

停止:

$ pm2 stop web-interface

As you can see the process hasn't disappeared. 它仍然在那不过是 stopped 状态.

重启:

$ pm2 restart web-interface

删除:

$ pm2 delete web-interface

也可通过配置文件定义选项

进程列表

列出所有运行进程:

$ pm2 list
# Or
$ pm2 [list|ls|l|status]

查看某一进程详情:

$ pm2 show 0

启动任何进程类型

其它语言脚本:

$ pm2 start echo.pl --interpreter=perl

$ pm2 start echo.coffee
$ pm2 start echo.php
$ pm2 start echo.py
$ pm2 start echo.sh
$ pm2 start echo.rb

The interpreter is set by default with this equivalence:

{
  ".sh": "bash",
  ".py": "python",
  ".rb": "ruby",
  ".coffee" : "coffee",
  ".php": "php",
  ".pl" : "perl",
  ".js" : "node"
}

JSON 配置

To run a non-JS interpreter you must set exec_mode to fork_mode and exec_interpreter to your interpreter of choice. For example:

{
  "apps" : [{
    "name"       : "bash-worker",
    "script"     : "./a-bash-script",
    "exec_interpreter": "bash",
    "exec_mode"  : "fork_mode"
  }, {
    "name"       : "ruby-worker",
    "script"     : "./some-ruby-script",
    "exec_interpreter": "ruby",
    "exec_mode"  : "fork_mode"
  }]
}

重启临界内存设置

PM2 allows to restart an application based on a memory limit.

CLI

$ pm2 start big-array.js --max-memory-restart 20M

JSON

{
  "name"   : "max_mem",
  "script" : "big-array.js",
  "max_memory_restart" : "20M"
}

编程

pm2.start({
  name               : "max_mem",
  script             : "big-array.js",
  max_memory_restart : "20M"
}, function(err, proc) {
  // Processing
});

单位

单位可以是 K(ilobyte), M(egabyte), G(igabyte).

50M
50K
1G