PM2 Process Management Quick StartPM2过程管理快速启动

PM2 is a daemon process manager that will help you manage and keep your application online. PM2是一个守护进程管理器,它将帮助您管理并保持应用程序在线。Getting started with PM2 is straightforward, it is offered as a simple and intuitive CLI, installable via NPM.PM2的入门非常简单,它是一个简单直观的CLI,可通过NPM安装。

Installation安装

The latest PM2 version is installable with NPM or Yarn:最新的PM2版本可与NPM或Yarn一起安装:

$ npm install pm2@latest -g
# or
$ yarn global add pm2

To install Node.js and NPM you can use NVM要安装Node.js和NPM,可以使用NVM

Start an app启动应用程序

The simplest way to start, daemonize and monitor your application is by using this command line:启动、监控应用程序的最简单方法是使用以下命令行:

$ pm2 start app.js

Or start any other application easily:或轻松启动任何其他应用程序:

$ pm2 start bashscript.sh
$ pm2 start python-app.py --watch
$ pm2 start binary-file -- --port 1520

Some options you can pass to the CLI:可以传递到CLI的一些选项:

# Specify an app name指定应用程序名称
--name <app_name>

# Watch and Restart app when files change当文件更改时,观察并重新启动应用程序
--watch

# Set memory threshold for app reload设置应用程序重新加载的内存阈值
--max-memory-restart <200MB>

# Specify log file指定日志文件
--log <log_path>

# Pass extra arguments to the script向脚本传递额外的参数
-- arg1 arg2 arg3

# Delay between automatic restarts自动重启之间的延迟
--restart-delay <delay in ms>

# Prefix logs with time为日志添加时间前缀
--time

# Do not auto restart app不要自动重启应用程序
--no-autorestart

# Specify cron for forced restart为强制重新启动指定cron
--cron <cron_pattern>

# Attach to application log附加到应用程序日志
--no-daemon

As you can see many options are available to manage your application with PM2. 正如您所见,有许多选项可用于使用PM2管理您的应用程序。You will discover them depending on your use case.您将根据您的用例发现它们。

Managing processes管理流程

Managing application state is simple here are the commands:管理应用程序状态很简单以下是命令:

$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name

Instead of app_name you can pass:您可以传递以下信息,而不是app_name

Check status, logs, metrics检查状态、日志、度量

Now that you have started this application, you can check its status, logs, metrics and even get the online dashboard with pm2.io.现在您已经启动了这个应用程序,您可以检查它的状态、日志、指标,甚至可以使用pm2.io获取在线仪表板。

List managed applications列出托管应用程序

List the status of all application managed by PM2:列出PM2管理的所有应用程序的状态:

$ pm2 [list|ls|status]

https://i.imgur.com/LmRD3FN.png

Display logs显示日志

To display logs in realtime:要实时显示日志,请执行以下操作:

$ pm2 logs

To dig in older logs:要挖掘旧日志,请执行以下操作:

$ pm2 logs --lines 200

Terminal Based Dashboard基于终端的仪表板

Here is a realtime dashboard that fits directly into your terminal:这是一个直接安装在终端上的实时仪表板:

$ pm2 monit

https://i.imgur.com/xo0LDb7.png

pm2.io: Monitoring & Diagnostic Web Interface监察及监察诊断Web界面

Web based dashboard, cross servers with diagnostic system:基于Web的仪表板,带诊断系统的跨服务器:

$ pm2 plus

https://i.imgur.com/sigMHli.png

Cluster mode集群模式

For Node.js applications, PM2 includes an automatic load balancer that will share all HTTP[s]/Websocket/TCP/UDP connections between each spawned processes.对于Node.js应用程序,PM2包含一个自动负载平衡器,该平衡器将在每个衍生进程之间共享所有HTTP/Websocket/TCP/UDP连接。

To start an application in Cluster mode:要以群集模式启动应用程序,请执行以下操作:

$ pm2 start app.js -i max

Read more about cluster mode here.阅读更多关于集群模式的信息。

Ecosystem File生态系统文件

You can also create a configuration file, called Ecosystem File, to manage multiple applications. 您还可以创建一个名为生态系统文件的配置文件来管理多个应用程序。To generate an Ecosystem file:要生成生态系统文件,请执行以下操作:

$ pm2 ecosystem

This will generate an ecosystem.config.js file:这将生成一个ecosystem.config.js文件:

module.exports = {
  apps : [{
    name: "app",
    script: "./app.js",
    env: {
      NODE_ENV: "development",
    },
    env_production: {
      NODE_ENV: "production",
    }
  }, {
     name: 'worker',
     script: 'worker.js'
  }]
}

And start it easily:并轻松启动:

$ pm2 start process.yml

Read more about application declaration here.请阅读有关应用程序声明的更多信息。

Setup startup script安装启动脚本

Restarting PM2 with the processes you manage on server boot/reboot is critical. 使用您在服务器引导/重新引导时管理的进程重新启动PM2至关重要。To solve this, just run this command to generate an active startup script:要解决此问题,只需运行此命令以生成活动启动脚本:

$ pm2 startup

And to freeze a process list for automatic respawn:以及冻结自动重生的流程列表:

$ pm2 save

Read more about startup script generator here.阅读更多关于启动脚本生成器的信息。

Restart application on changes根据更改重新启动应用程序

It’s pretty easy with the --watch option:使用--watch选项非常简单:

$ cd /path/to/my/app
$ pm2 start env.js --watch --ignore-watch="node_modules"

This will watch & restart the app on any file change from the current directory + all subfolders and it will ignore any changes in the node_modules folder --ignore-watch="node_modules".这将对在当前目录+所有子文件夹中的任何文件更改进行监视和重新启动应用程序,它将忽略节点模块文件夹--ignore-watch="node_modules"中的任何更改。

You can then use pm2 logs to check for restarted app logs.然后,您可以使用pm2 logs检查重新启动的应用程序日志。

Updating PM2更新PM2

We made it simple, there is no breaking change between releases and the procedure is straightforward:我们让它变得简单,在发布之间没有突破性的变化,过程也很简单:

npm install pm2@latest -g

Then update the in-memory PM2 :然后更新内存中的PM2:

pm2 update

CheatSheet备忘单

Here are some commands that are worth knowing. 下面是一些值得了解的命令。Just try them with a sample application or with your current web application on your development machine:只需使用示例应用程序或开发机器上当前的web应用程序进行尝试:

# Fork mode
pm2 start app.js --name my-api # Name process命名过程

# Cluster mode
pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs将使用LB启动最大进程,具体取决于可用CPU
pm2 start app.js -i max      # Same as above, but deprecated.同上,但已弃用。
pm2 scale app +3             # Scales `app` up by 3 workers将`app`扩展3个工作线程
pm2 scale app 2              # Scales `app` up or down to 2 workers total将`app`向上或向下扩展至总共2名工作线程

# Listing

pm2 list               # Display all processes status显示所有进程状态
pm2 jlist              # Print process list in raw JSON以原始JSON打印进程列表
pm2 prettylist         # Print process list in beautified JSON用美化的JSON打印进程列表

pm2 describe 0         # Display all informations about a specific process显示有关特定流程的所有信息

pm2 monit              # Monitor all processes监视所有进程

# Logs

pm2 logs [--raw]       # Display all processes logs in streaming显示流式处理中的所有进程日志
pm2 flush              # Empty all log files清空所有日志文件
pm2 reloadLogs         # Reload all logs重新加载所有日志

# Actions

pm2 stop all           # Stop all processes停止所有进程
pm2 restart all        # Restart all processes重新启动所有进程

pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)将重新加载(对于网络应用程序)

pm2 stop 0             # Stop specific process id停止特定进程id
pm2 restart 0          # Restart specific process id重新启动特定进程id

pm2 delete 0           # Will remove process from pm2 list将从pm2列表中删除流程
pm2 delete all         # Will remove all processes from pm2 list将从pm2列表中删除所有流程

# Misc

pm2 reset <process>    # Reset meta data (restarted time...)重置元数据(重新启动时间…)
pm2 updatePM2          # Update in memory pm2内存pm2中的更新
pm2 ping               # Ensure pm2 daemon has been launched确保pm2守护进程已启动
pm2 sendSignal SIGUSR2 my-app # Send system signal to script向脚本发送系统信号
pm2 start app.js --no-daemon
pm2 start app.js --no-vizion
pm2 start app.js --no-autorestart

What’s next?下一步是什么?

Learn how to declare all your application’s behavior options into a JSON configuration file.了解如何将应用程序的所有行为选项声明为JSON配置文件

Learn how to do clean stop and restart to increase reliability.了解如何进行清空停止和重新启动以提高可靠性。

Learn how to deploy and update production applications easily.了解如何轻松部署和更新生产应用程序

Monitor your production applications with PM2.io.使用PM2.io监控生产应用程序。

How to update PM2如何更新PM2

Install the latest pm2 version:安装最新的pm2版本:

npm install pm2@latest -g

Then update the in-memory PM2 :然后更新内存中的PM2:

pm2 update