koajs

高级管理

PM2

主版: Build Status 开发版: Build Status

 菜单

入门

功能

部署 - ecosystem.json

使用 PM2 编程 (via API)

具体

知识

更多


入门

安装

PM2的稳定版可以通过NPM安装:

$ npm install pm2@latest -g

如果上面失败,可以使用:

$ npm install git://github.com/Unitech/pm2#master -g

We recommend Node.JS 0.11.14 for handling the cluster_mode (if you add the -i options to enable scaling and reload).

使用

Hello world:

$ pm2 start app.js

原生实例

# 叉模式
$ pm2 start app.js --name my-api # Name process

# 集群模式
$ pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs
$ pm2 start app.js -i max      # Same as above, but deprecated yet.

# 列表

$ pm2 list               # Display all processes status
$ pm2 jlist              # Print process list in raw JSON
$ pm2 prettylist         # Print process list in beautified JSON

$ pm2 describe 0         # Display all informations about a specific process

$ pm2 monit              # Monitor all processes

# 日志

$ pm2 logs               # Display all processes logs in streaming
$ pm2 ilogs              # Advanced termcaps interface to display logs
$ pm2 flush              # Empty all log file
$ pm2 reloadLogs         # Reload all logs

# 动作

$ pm2 stop all           # Stop all processes
$ pm2 restart all        # Restart all processes

$ pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)
$ pm2 gracefulReload all # Send exit message then reload (for networked apps)

$ pm2 stop 0             # Stop specific process id
$ pm2 restart 0          # Restart specific process id

$ pm2 delete 0           # Will remove process from pm2 list
$ pm2 delete all         # Will remove all processes from pm2 list

# 杂项

$ pm2 reset <process>    # Reset meta data (restarted time...)
$ pm2 updatePM2          # Update in memory pm2
$ pm2 ping               # Ensure pm2 daemon has been launched
$ pm2 sendSignal SIGUSR2 my-app # Send system signal to script
$ pm2 start app.js --no-daemon

不同的方式来启动一个进程

$ pm2 start app.js           # Start app.js

$ pm2 start app.js -- -a 23  # Pass arguments '-a 23' argument to app.js script

$ pm2 start app.js --name serverone # Start a process an name it as server one
                                    # you can now stop the process by doing
                                    # pm2 stop serverone

$ pm2 start app.js --node-args="--debug=7001" # --node-args to pass options to node V8

$ pm2 start app.js -i 0             # Start maximum processes depending on available CPUs (cluster mode)

$ pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm Z"    # Log will be prefixed with custom time format

$ pm2 start app.json                # Start processes with options declared in app.json
                                    # Go to chapter Multi process JSON declaration for more

$ pm2 start app.js -e err.log -o out.log  # Start and specify error and out log

其它语言的脚本:

$ 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"
}

选项

选项:

   -h, --help                           output usage information
   -V, --version                        output the version number
   -v --version                         get version
   -s --silent                          hide all messages
   -m --mini-list                       display a compacted list without formatting
   -f --force                           force actions
   -n --name <name>                     set a <name> for script
   -i --instances <number>              launch [number] instances (for networked app)(load balanced)
   -l --log [path]                      specify entire log file (error and out are both included)
   -o --output <path>                   specify out log file
   -e --error <path>                    specify error log file
   -p --pid <pid>                       specify pid file
   --max-memory-restart <memory>        specify max memory amount used to autorestart (in megaoctets)
   --env <environment_name>             specify environment to get specific env variables (for JSON declaration)
   -x --execute-command                 execute a program using fork system
   -u --user <username>                 define user when generating startup script
   -c --cron <cron_pattern>             restart a running process based on a cron pattern
   -w --write                           write configuration in local folder
   --interpreter <interpreter>          the interpreter pm2 should use for executing app (bash, python...)
   --log-date-format <momentjs format>  add custom prefix timestamp to logs
   --no-daemon                          run pm2 daemon in the foreground if it doesn't exist already
   --merge-logs                         merge logs from different instances but keep error and out separated
   --watch                              watch application folder for changes
   --ignore-watch <folders|files>       folder/files to be ignored watching, chould be a specific name or regex - e.g. --ignore-watch="test node_modules "some scripts""
   --node-args <node_args>              space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation"
   --no-color                           skip colors

架构

完整定义:

{
  "script": {
    "type": "string",
    "require": true
  },
  "args": {
    "type": [
      "array",
      "string"
    ]
  },
  "node_args": {
    "type": [
      "array",
      "string"
    ]
  },
  "name": {
    "type": "string"
  },
  "max_memory_restart": {
    "type": [
      "string",
      "number"
    ],
    "regex": "^\\d+(G|M|K)?$",
    "ext_type": "sbyte",
    "desc": "it should be a NUMBER - byte, \"[NUMBER]G\"(Gigabyte), \"[NUMBER]M\"(Megabyte) or \"[NUMBER]K\"(Kilobyte)"
  },
  "instances": {
    "type": "number",
    "min": 0
  },
  "log_file": {
    "type": [
      "boolean",
      "string"
    ],
    "alias": "log"
  },
  "error_file": {
    "type": "string",
    "alias": "error"
  },
  "out_file": {
    "type": "string",
    "alias": "output"
  },
  "pid_file": {
    "type": "string",
    "alias": "pid"
  },
  "cron_restart": {
    "type": "string",
    "alias": "cron"
  },
  "cwd": {
    "type": "string"
  },
  "merge_logs": {
    "type": "boolean"
  },
  "watch": {
    "type": "boolean"
  },
  "ignore_watch": {
    "type": [
      "array",
      "string"
    ]
  },
  "env": {
    "type": ["object", "string"]
  },
  "^env_\\S*$": {
    "type": [
      "object",
      "string"
    ]
  },
  "log_date_format": {
    "type": "string"
  },
  "min_uptime": {
    "type": [
      "number",
      "string"
    ],
    "regex": "^\\d+(h|m|s)?$",
    "desc": "it should be a NUMBER - milliseconds, \"[NUMBER]h\"(hours), \"[NUMBER]m\"(minutes) or \"[NUMBER]s\"(seconds)",
    "min": 100,
    "ext_type": "stime"
  },
  "max_restarts": {
    "type": "number",
    "min": 0
  },
  "exec_mode": {
    "type": "string",
    "regex": "^(cluster|fork)(_mode)?$",
    "alias": "executeCommand",
    "desc": "it should be \"cluster\"(\"cluster_mode\") or \"fork\"(\"fork_mode\") only"
  },
  "exec_interpreter": {
    "type": "string",
    "alias": "interpreter"
  },
  "write": {
    "type": "boolean"
  },
  "force": {
    "type": "boolean"
  }
}

All the keys can be used in a JSON configured file, and just need to make a small change in CLI, e.g.:

exec_interpreter  -> --interpreter
exec_mode         -> --execute_command
max_restarts      -> --max_restarts
force             -> --force

Yap, if the alias exists, you can using it as a CLI option, but do not forget to turn the camelCasing to underscore split - executeCommand to --execute_command.

注意

  • Using quote to make an ESC, e.g.:

    $pm2 start test.js --node-args "port=3001 sitename='first pm2 app'"
    

    The nodeArgs will be

    [
      "port=3001",
      "sitename=first pm2 app"
    ]
    

    But not

    [
      "port=3001",
      "sitename='first",
      "pm2",
      "app'"
    ]
    
  • RegExp key

    Matches the keys of configured JSON by RegExp but not a specific String, e.g. ^env_\\S*$ will match all env keys like env_production, env_test, and make sure the values conform to the schemas.

  • Special ext_type

    • min_uptime

      Value of min_uptime could be:

      • Number e.g. "min_uptime": 3000 means 3000 milliseconds.
      • String In the meantime we are making it briefness and easy configuration: h, m and s, e.g.: "min_uptime": "1h" means one hour, "min_uptime": "5m" means five minutes and "min_uptime": "10s" means ten seconds (At last, it will be transformed into milliseconds).
    • max_memory_restart

      Value of max_memory_restart could be:

      • Number e.g. "max_memory_restart": 1024 means 1024 bytes (NOT BITS).
      • String In the meantime we are making it briefness and easy configuration: G, M and K, e.g.: "max_memory_restart": "1G" means one gigabytes, "max_memory_restart": "5M" means five megabytes and "max_memory_restart": "10K" means ten kilobytes (At last, it will be transformed into byte(s)).
  • Optional values

    Like exec_mode, value could be one of cluster (cluster_mode) or fork (fork_mode) only.

  • Should known

    • maximum

      "instances": 0 means starting maximum processes depending on available CPUs (cluster mode)

如何更新PM2

安装最新pm2版本 :

$ npm install pm2@latest -g

然后更新内存中的PM2:

$ pm2 updatePM2

功能

应用程序的转换状态 (重要)

PM2是进程管理器. PM2可以启动停止重启和删除进程.

启动进程:

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

现在需要停止web-interface:

$ pm2 stop web-interface

你可以看到 the process hasn't disappeared. 仍然在那但是 stopped 状态.

重启它:

$ pm2 restart web-interface

现在我要从PM2进程列表 删除 这个应用:

$ pm2 delete web-interface

进程列表

Monit

列出所有运行进程:

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

列出指定进程的详细信息:

$ pm2 describe 0

Automatic restart process based on memory

Value passed is in megaoctets. Internally it uses the V8 flag --max-old-space-size=MEM to make a process exit when memory exceed a certain amount of RAM used.

CLI:

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

JSON:

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

Units can be K(ilobyte), M(egabyte), G(igabyte).

监控CPU/内存使用率

Monit

监控启动的所有进程:

$ pm2 monit

日志管理

启用pm2.log的时间戳前缀

export PM2_LOG_DATE_FORMAT="YYYY-MM-DD HH:mm Z"

If this env-variable has been changed, you need to dump your processes and kill daemon, restart it again to take effect, e.g.:

$ pm2 dump
$ pm2 [resurrect|save]

实时显示日志

Monit

Displaying logs of specified process or all processes in realtime:

$ pm2 logs
$ pm2 logs big-api
$ pm2 flush # Clear all the logs

高级日志接口

Navigate between processes logs in realtime with an ergonomic interface:

$ pm2 ilogs

重载所有日志 (SIGUSR2/Logrotate)

To reload all logs, you can send SIGUSR2 to the PM2 process.

You can also reload all logs via the command line with:

$ pm2 reloadLogs

选项

--merge-logs : merge logs from different instances but keep error and out separated
--log-date-format <format>: prefix logs with formated timestamp (http://momentjs.com/docs/#/parsing/string-format/)

合并 outerr 日志

  • If you only want to merge out and err logs into one output file, try with the following examples:

    $ pm2 start name.js -o name.log -e name.log
    
    {
      "apps" : [{
        "name"        : "name",
        "script"      : "name.js",
        "err_file"    : "name.log",
        "out_file"    : "name.log"
      }]
    }
    
  • How about merge out and err logs into one, and also keep the separated logs? e.g.:

    $ pm2 start -l
    
    $ pm2 start -l name.log
    
    {
      "apps" : [{
        "name"        : "name",
        "script"      : "name.js",
        "log_file"    : true
      }]
    }
    
    {
      "apps" : [{
        "name"        : "name",
        "script"      : "name.js",
        "log_file"    : "name.log"
      }]
    }
    

    Notes: When you providing a Boolean (true) value for -l, --log option or log_file property, it means a merged log file will be automatic generated, i.e. ~/.pm2/logs/[name]-[id].log, otherwise it's specific.

集群 (cluster_mode)

The cluster_mode will automatically wrap your Node.js app into the cluster module and will enable you to reload your app without downtime and to scale your processes across all CPUs available.

To enable the cluster_mode, just pass the -i option:

$ pm2 start app.js -i 1

To launch max instances (max depending on the number of CPUs available) and set the load balancer to balance queries among process:

$ pm2 start app.js --name "API" -i 0

DEPRECATED (STILL COMPATIBLE):

$ pm2 start app.js --name "API" -i max

If your app is well-designed (stateless) you'll be able to process many more queries.

Important concepts to make a Node.js app stateless:

热重载

As opposed to restart, which kills and restarts the process, reload achieves a 0-second-downtime reload.

Warning This feature only works for apps in cluster_mode, that uses HTTP/HTTPS/Socket connections.

重载应用:

$ pm2 reload api

If the reload system hasn't managed to reload your app, a timeout will simply kill the process and will restart it.

优雅重载

Sometimes you can experience a very long reload, or a reload that doesn't work (fallback to restart).

It means that your app still has open connections on exit.

To work around this problem you have to use the graceful reload. Graceful reload is a mechanism that will send a shutdown message to your process before reloading it. You can control the time that the app has to shutdown via the PM2_GRACEFUL_TIMEOUT environment variable.

Example:

process.on('message', function(msg) {
  if (msg == 'shutdown') {
    // Your process is going to be reloaded
    // You have to close all database/socket.io/* connections

    console.log('Closing all connections...');

    // You will have 4000ms to close all connections before
    // the reload mechanism will try to do its job

    setTimeout(function() {
      console.log('Finished closing connections');
      // This timeout means that all connections have been closed
      // Now we can exit to let the reload mechanism do its job
      process.exit(0);
    }, 1500);
  }
});

Then use the command:

$ pm2 gracefulReload [all|name]

When PM2 starts a new process to replace an old one, it will wait for the new process to begin listening to a connection or a timeout before sending the shutdown message to the old one. You can define the timeout value with the PM2_GRACEFUL_LISTEN_TIMEOUT environamente variable. I f a script does not need to listen to a connection, it can manually tell PM2 that the process has started up by calling process.send('online').

启动脚本

PM2 has the amazing ability to generate startup scripts and configure them. PM2 is also smart enough to save all your process list and to bring back all your processes on restart.

$ pm2 startup [ubuntu|centos|gentoo|systemd]

Once you have started the apps and want to keep them on server reboot do:

$ pm2 save

Warning It's tricky to make this feature work generically, so once PM2 has setup your startup script, reboot your server to make sure that PM2 has launched your apps!

更多信息

Three types of startup scripts are available:

  • SystemV init script (with the option ubuntu or centos)
  • OpenRC init script (with the option gentoo)
  • SystemD init script (with the systemd option)

The startup options are using:

  • ubuntu will use updaterc.d and the script lib/scripts/pm2-init.sh
  • centos will use chkconfig and the script lib/scripts/pm2-init-centos.sh
  • gentoo will use rc-update and the script lib/scripts/pm2
  • systemd will use systemctl and the script lib/scripts/pm2.service

用户权限

Let's say you want the startup script to be executed under another user.

Just use the -u <username> option !

$ pm2 startup ubuntu -u www

相关命令

Dump all processes status and environment managed by PM2:

$ pm2 dump

It populates the file ~/.pm2/dump.pm2 by default.

To bring back the latest dump:

$ pm2 [resurrect|save]

监控 & 重启

PM2 can automatically restart your app when a file changes in the current directory or its subdirectories:

$ pm2 start app.js --watch

If --watch is enabled, stopping it won't stop watching:

  • pm2 stop 0 'll not stop watching
  • pm2 stop --watch 0 'll stop watching

Restart toggle the watch parameter when triggered.

To watch specific paths, please use a JSON app declaration, watch can take a string or an array of paths. Default is true:

{
  "watch": ["server", "client"],
  "ignoreWatch" : ["node_modules", "client/img"]
}

JSON app 声明

You can define parameters for your apps in processes.json:

{
  "apps" : [{
    "name"        : "echo",
    "script"      : "examples/args.js",
    "args"        : "['--toto=heya coco', '-d', '1']",
    "log_date_format"  : "YYYY-MM-DD HH:mm Z",
    "ignoreWatch" : ["[\\/\\\\]\\./", "node_modules"],
    "watch"       : true,
    "node_args"   : "--harmony",
    "cwd"         : "/this/is/a/path/to/start/script",
    "env": {
        "NODE_ENV": "production",
        "AWESOME_SERVICE_API_TOKEN": "xxx"
    }
  },{
    "name"       : "api",
    "script"     : "./examples/child.js",
    "instances"  : "4",
    "log_date_format"  : "YYYY-MM-DD",
    "log_file"   : "./examples/child.log",
    "error_file" : "./examples/child-err.log",
    "out_file"   : "./examples/child-out.log",
    "pid_file"   : "./examples/child.pid",
    "exec_mode"  : "cluster_mode",
    "port"       : 9005
  },{
    "name"       : "auto-kill",
    "script"     : "./examples/killfast.js",
    "min_uptime" : "100",
    "exec_mode"  : "fork_mode"
  }]
}

Then run:

$ pm2 start processes.json
$ pm2 stop processes.json
$ pm2 delete processes.json
$ pm2 restart processes.json

A few notes about JSON app declarations:

  • All command line options passed when using the JSON app declaration will be dropped i.e.
$ cat node-app-1.json

{
  "name" : "node-app-1",
  "script" : "app.js",
  "cwd" : "/srv/node-app-1/current"
}
  • JSON app declarations are additive. Continuing from above:

    $ pm2 start node-app-2.json
    $ ps aux | grep node-app
    root  14735  5.8  1.1  752476  83932 ? Sl 00:08 0:00 pm2: node-app-1
    root  24271  0.0  0.3  696428  24208 ? Sl 17:36 0:00 pm2: node-app-2
    

    Note that if you execute pm2 start node-app-2 again, it will spawn an additional instance node-app-2.

  • cwd: your JSON declaration does not need to reside with your script. If you wish to maintain the JSON(s) in a location other than your script (say, /etc/pm2/conf.d/node-app.json) you will need to use the cwd feature. (Note, this is especially helpful for capistrano style directory structures that use symlinks.) Files can be either relative to the cwd directory, or absolute (example below.)

  • The following are valid options for JSON app declarations:

    [{
    "name"             : "node-app",
    "cwd"              : "/srv/node-app/current",
    "args"             : "['--toto=heya coco', '-d', '1']",
    "script"           : "bin/app.js",
    "node_args"        : ["--harmony", " --max-stack-size=102400000"],
    "log_date_format"  : "YYYY-MM-DD HH:mm Z",
    "error_file"       : "/var/log/node-app/node-app.stderr.log",
    "out_file"         : "log/node-app.stdout.log",
    "pid_file"         : "pids/node-geo-api.pid",
    "instances"        : 6, //or 0 => 'max'
    "min_uptime"       : "200s", // 200 seconds, defaults to 1000
    "max_restarts"     : 10, // defaults to 15
    "max_memory_restart": "1M", // 1 megabytes, e.g.: "2G", "10M", "100K", 1024... the default unit is byte.
    "cron_restart"     : "1 0 * * *",
    "watch"            : false,
    "ignoreWatch"      : ["[\\/\\\\]\\./", "node_modules"],
    "merge_logs"       : true,
    "exec_interpreter" : "node",
    "exec_mode"        : "fork",
    "env": {
      "NODE_ENV": "production",
      "AWESOME_SERVICE_API_TOKEN": "xxx"
    }
    }]
    

部署

PM2 embed a simple and powerful deployment system with revision tracing. It's based on https://github.com/visionmedia/deploy

A step-by-step tutorial is available here : Deploy and Iterate faster with PM2 deploy

Getting started with deployment

Please read the Considerations to use PM2 deploy

1- Generate a sample ecosystem.json file that list processes and deployment environment

$ pm2 ecosystem

In the current folder a ecosystem.json file will be created. It contains this:

{
  "apps" : [{
    "name"      : "API",
    "script"    : "app.js",
    "env": {
      "COMMON_VARIABLE": "true"
    },
    "env_production" : {
      "NODE_ENV": "production"
    }
  },{
    "name"      : "WEB",
    "script"    : "web.js"
  }],
  "deploy" : {
    "production" : {
      "user" : "node",
      "host" : "212.83.163.1",
      "ref"  : "origin/master",
      "repo" : "[email protected]:repo.git",
      "path" : "/var/www/production",
      "post-deploy" : "pm2 startOrRestart ecosystem.json --env production"
    },
    "dev" : {
      "user" : "node",
      "host" : "212.83.163.1",
      "ref"  : "origin/master",
      "repo" : "[email protected]:repo.git",
      "path" : "/var/www/development",
      "post-deploy" : "pm2 startOrRestart ecosystem.json --env dev",
      "env"  : {
        "NODE_ENV": "dev"
      }
    }
  }
}

Edit the file according to your needs.

2- Be sure that you have the public ssh key on your local machine

$ ssh-keygen -t rsa
$ ssh-copy-id [email protected]

3- Now initialize the remote folder with:

$ pm2 deploy <configuration_file> <environment> setup

E.g:

$ pm2 deploy ecosystem.json production setup

This command will create all the folders on your remote server.

4- Deploy your code

$ pm2 deploy ecosystem.json production

Now your code will be populated, installed and started with PM2

Deployment options

Display deploy help via pm2 deploy help:

$ pm2 deploy <configuration_file> <environment> <command>

  Commands:
    setup                run remote setup commands
    update               update deploy to the latest release
    revert [n]           revert to [n]th last deployment or 1
    curr[ent]            output current release commit
    prev[ious]           output previous release commit
    exec|run <cmd>       execute the given <cmd>
    list                 list previous deploy commits
    [ref]                deploy to [ref], the "ref" setting, or latest tag

命令

$ pm2 startOrRestart all.json            # Invoke restart on all apps in JSON
$ pm2 startOrReload all.json             # Invoke reload
$ pm2 startOrGracefulReload all.json     # Invoke gracefulReload

注意事项

  • You might want to commit your node_modules folder (#622) or add the npm install command to the post-deploy section: "post-deploy" : "npm install && pm2 startOrRestart ecosystem.json --env production"
  • Verify that your remote server has the permission to git clone the repository
  • You can declare specific environment variable depending on the environment you want to deploy the code to. For instance to declare variables for the production environment, just add "env_production": {} and declare that variables.
  • PM2 will look by default to ecosystem.json. So you can skip the options if it's the case
  • You can embed the "apps" & "deploy" section in the package.json
  • It deploys your code via ssh, you don't need any dependencies
  • Process are initialized / started automatically depending on application name in ecosystem.json
  • PM2-deploy repository is there: pm2-deploy

贡献

The module is https://github.com/Unitech/pm2-deploy Feel free to PR for any changes or fix.

Using PM2 programmatically

PM2 can be used programmatically, meaning that you can embed a process manager directly in your code, spawn processes, keep them alive even if the main script is exited.

Check out this article for more informations.

简单实例

This will require pm2, launch test.js, list processes then exit the script. You will notice that after exiting this script you will be able to see test.js process with pm2 list

$ npm install pm2 --save
var pm2 = require('pm2');

// Connect or launch PM2
pm2.connect(function(err) {

  // Start a script on the current folder
  pm2.start('test.js', { name: 'test' }, function(err, proc) {
    if (err) throw new Error('err');

    // Get all processes running
    pm2.list(function(err, process_list) {
      console.log(process_list);

      // Disconnect to PM2
      pm2.disconnect(function() { process.exit(0) });
    });
  });
})

编程API

Method name API
Connect/Launch pm2.connect(fn(err){})
Disconnect pm2.disconnect(fn(err, proc){})

Consideration with .connect: the .connect method connect to the local PM2, but if PM2 is not up, it will launch it and will put in in background as you launched it via CLI.

Method name API
Start pm2.start(script_path|json_path, options, fn(err, proc){})
Options nodeArgs(arr), scriptArgs(arr), name(str), instances(int), error(str), output(str), pid(int), cron(str), mergeLogs(bool), watch(bool), runAsUser(int), runAsGroup(int), executeCommand(bool), interpreter(str), write(bool)
Restart pm2.restart(proc_name|proc_id|all, fn(err, proc){})
Stop pm2.stop(proc_name|proc_id|all, fn(err, proc){})
Delete pm2.delete(proc_name|proc_id|all, fn(err, proc){})
Reload pm2.reload(proc_name|all, fn(err, proc){})
Graceful Reload pm2.gracefulReload(proc_name|all, fn(err, proc){})
Method name API
List pm2.list(fn(err, list){})
Describe process pm2.describe(proc_name|proc_id, fn(err, list){})
Dump (save) pm2.dump(fn(err, ret){})
Flush logs pm2.flush(fn(err, ret){})
Reload logs pm2.reloadLogs(fn(err, ret){})
Send signal pm2.sendSignalToProcessName(signal,proc,fn(err, ret){})
Generate start script pm2.startup(platform, fn(err, ret){})
Kill PM2 pm2.killDaemon(fn(err, ret){})

特殊功能

Launching PM2 without daemonizing itself:

$ pm2 start app.js --no-daemon

Sending a system signal to a process:

$ pm2 sendSignal SIGUSR2 my-app

配置文件

You can specify the following options by editing the file ~/.pm2/custom_options.sh:

PM2_RPC_PORT
PM2_PUB_PORT
PM2_BIND_ADDR
PM2_API_PORT
PM2_GRACEFUL_TIMEOUT
PM2_MODIFY_REQUIRE

API health endpoint

$ pm2 web

启动 Harmony ES6

The --node-args option permit to launch script with V8 flags, so to enable harmony for a process just do this:

$ pm2 start api.js --node-args="--harmony"

And with JSON declaration:

[{
  "name" : "ES6",
  "script" : "es6.js",
  "node_args" : "--harmony"
}]

CoffeeScript

$ pm2 start my_app.coffee

That's all!

Knowledge

无状态的应用程序

We recommend (and you must) write stateless NodeJS apps. Apps that don't retain any form of local variables or local instances or whatever local. Every data, states, websocket session, session data, must be shared via any kind of database.

We recommend using Redis for sharing session data, websocket.

We recommend following the 12 factor convention : http://12factor.net/

在服务器上设置pm2

How To Use pm2 to Setup a Node.js Production Environment On An Ubuntu VPS

日志和PID文件

By default, logs (error and output), pid files, dumps, and PM2 logs are located in ~/.pm2/:

.pm2/
├── dump.pm2
├── custom_options.sh
├── pm2.log
├── pm2.pid
├── logs
└── pids

执行任何类型脚本

In fork mode almost all options are the same as the cluster mode. But there is no reload or gracefulReload command.

You can also exec scripts written in other languages:

$ pm2 start my-bash-script.sh -x --interpreter bash

$ pm2 start my-python-script.py -x --interpreter python

The interpreter is deduced from the file extension from the following list.

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"
  }]
}

JSON应用配置通过管道从stdout

Pull-requests:

#!/bin/bash

read -d '' my_json <<_EOF_
[{
    "name"       : "app1",
    "script"     : "/home/projects/pm2_nodetest/app.js",
    "instances"  : "4",
    "error_file" : "./logz/child-err.log",
    "out_file"   : "./logz/child-out.log",
    "pid_file"   : "./logz/child.pid",
    "exec_mode"  : "cluster_mode",
    "port"       : 4200
}]
_EOF_

echo $my_json | pm2 start -

我的生产服务器为PM2准备好了吗?

Just try the tests before using PM2 on your production server

$ git clone https://github.com/Unitech/pm2.git
$ cd pm2
$ npm install  # Or do NODE_ENV=development npm install if some packages are missing
$ npm test

If a test is broken please report us issues here Also make sure you have all dependencies needed. For Ubuntu:

$ sudo apt-get install build-essential
# nvm is a Node.js version manager - https://github.com/creationix/nvm
$ wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh
$ nvm install v0.11.14
$ nvm use v0.11.14
$ nvm alias default v0.11.14

贡献/开发模式

To hack PM2, it's very simple:

$ pm2 kill   # kill the current pm2
$ git clone my_pm2_fork.git
$ cd pm2/
$ DEBUG=* PM2_DEBUG=true ./bin/pm2 --no-daemon

Each time you edit the code, be sure to kill and restart PM2 to make changes taking effect.

安装 PM2 开发版

$ npm install git://github.com/Unitech/pm2#development -g

已知的错误和解决方法

First, install the lastest PM2 version:

$ npm install -g pm2@latest

Node 0.10.x doesn't free the script port when stopped in cluster_mode

Don't use the cluster_mode via -i option.

来自问题的用户建议

外部资源和文章

贡献者

Contributors

赞助商

Thanks to Devo.ps and Wiredcraft for their knowledge and expertise.

证书

Files in lib/ are made available under the terms of the GNU Affero General Public License 3.0 (AGPL 3.0). Except the file lib/CLI.js who is made under the terms of the Apache V2 license.