15v-pm2 中文文档教程

发布于 7年前 浏览 26 项目主页 更新于 3年前


pm2 logo

P(rocess) M(anager) 2
Runtime Edition

npm version NPM Downloads Build Status


PM2 是一个通用流程管理器和一个用于 Node.js 应用程序的生产运行时,具有内置的负载均衡器。

主要特点:

  • Simple and efficient process management (start/stop/restart/delete/show/monit)
  • Keep your application ALWAYS ONLINE with auto restarts and init system script generation
  • Clusterize Node.js Applications without code change to increase performance and reliability
  • Hot Reload Node.js Applications without extra configuration

在生产模式下启动应用程序非常简单:

$ pm2 start app.js

PM2 不断受到超过 1800 次测试 的攻击。

官方网站:http://pm2.keymetrics.io/

适用于 Linux(稳定版)& macOS(稳定)和 Windows(稳定)。 从 Node.js 0.12 开始支持所有 Node.js 版本。

NPM

Installing PM2

$ npm install pm2 -g

npm 是安装 Node.js 时的内置 CLI - 使用 NVM 安装 Node.js

Start an application

您可以像这样启动任何应用程序(Node.js、Python、Ruby、$PATH 中的二进制文件……):

$ pm2 start app.js

您的应用程序现已被守护进程,监控并永远保持活力。

关于流程管理的更多信息

Managing a Process

应用程序启动后,您可以轻松管理它们:

进程列表

列出所有正在运行的进程:

$ pm2 list

管理进程很简单:

$ pm2 stop     <app_name|id|'all'|json_conf>
$ pm2 restart  <app_name|id|'all'|json_conf>
$ pm2 delete   <app_name|id|'all'|json_conf>

了解特定进程的更多详细信息:

$ pm2 describe <id|app_name>

要 监控日志、自定义指标、进程信息:

$ pm2 monit

更多关于进程管理

Cluster Mode: Node.js Load Balancing & Hot Reload

启动一个 Node.js 应用程序,它会启动多个进程并在它们之间负载平衡 HTTP/TCP/UDP 查询。 这提高了整体性能(在 16 核机器上提高了 10 倍)和可靠性(在出现未处理错误的情况下更快地重新平衡套接字)。

在集群模式下启动一个 Node.js 应用程序,它将利用所有可用的 CPU:

$ pm2 start api.js -i <processes>

可以是 'max'-1 (所有 cpu 减去 1) 或指定数量的实例启动。

Hot Reload

Hot Reload 允许在不停机的情况下更新应用程序:

$ pm2 reload all

所有主要 Node.js 框架和任何 Node.js 应用程序的无缝支持,无需任何代码更改:

支持的框架

有关 PM2 如何简化集群的更多信息

Container Support

使用 node 的插入式替换命令,称为 pm2-runtime,运行您的节点。 js 应用程序在适当的生产环境中。 我们还提供了一个官方支持的 Docker 镜像

使用它是无缝的:

FROM keymetrics/pm2:latest-alpine
[...]
CMD [ "pm2-runtime", "npm", "--", "start" ]

阅读有关专用集成的更多信息

Terminal Based Monitoring

Monit

监控直接从命令行启动的所有进程:

$ pm2 monit

Monitor PM2 and Applications with our SaaS

在生产中部署应用程序后,您可以监控、调试并使用我们的 SaaS Monitoring 在外部对其进行分析。

要从终端开始监控应用程序:

$ pm2 register

有关 PM2 监控的更多信息

Expose Custom Metrics

要更深入地了解应用程序的行为,请在代码中插入自定义指标,然后使用 pm2 monit 命令监控它们:

在您的项目中安装 pmx

$ npm install pmx --save

然后插入自定义指标:

var Probe = require('pmx').probe();

var counter = 1;

var metric = Probe.metric({
  name    : 'Counter',
  value   : function() {
    return counter;
  }
});

setInterval(function() {
  counter++;
}, 1000);

然后从终端中查看指标类型:

$ pm2 monitor

指标、计数器、直方图和计量表 可用

Log facilities

Monit

实时显示指定进程或所有进程的日志,很容易:

$ pm2 logs ['all'|app_name|app_id] [--json] [--format] [--raw]

标准、原始、JSON 和格式化输出可用。

示例:

$ pm2 logs APP-NAME       # Display APP-NAME logs
$ pm2 logs --json         # JSON output
$ pm2 logs --format       # Formated output

$ pm2 flush               # Flush all logs
$ pm2 reloadLogs          # Reload all logs

更多关于日志管理

Startup script generation

PM2 可以生成和配置启动脚本以保持 PM2 和您的进程处于活动状态在每次服务器重新启动时。

支持初始化系统,如:systemd(Ubuntu 16、CentOS、Arch)、upstart(Ubuntu 14/12)、launchd(MacOSx、Darwin) , rc.d (FreeBSD)。

# Auto detect init system + generate and setup PM2 boot at server startup
$ pm2 startup

# Manually specify the startup system
# Can be: systemd, upstart, launchd, rcd
$ pm2 startup [platform]

# Disable and remove PM2 boot at server startup
$ pm2 unstartup

要在重启时保存/冻结进程列表:

$ pm2 save

有关启动脚本

Commands Cheatsheet

# General
$ npm install pm2 -g            # Install PM2
$ pm2 start app.js              # Start, Daemonize and auto-restart application (Node)
$ pm2 start app.py              # Start, Daemonize and auto-restart application (Python)
$ pm2 start npm -- start        # Start, Daemonize and auto-restart Node application

# Cluster Mode (Node.js only)
$ pm2 start app.js -i 4         # Start 4 instances of application in cluster mode
                                # it will load balance network queries to each app
$ pm2 reload all                # Zero Second Downtime Reload
$ pm2 scale [app-name] 10       # Scale Cluster app to 10 process

# Process Monitoring
$ pm2 list                      # List all processes started with PM2
$ pm2 list --sort=<field>       # Sort all processes started with PM2
$ pm2 monit                     # Display memory and cpu usage of each app
$ pm2 show [app-name]           # Show all information about application

# Log management
$ pm2 logs                      # Display logs of all apps
$ pm2 logs [app-name]           # Display logs for a specific app
$ pm2 logs --json               # Logs in JSON format
$ pm2 flush
$ pm2 reloadLogs

# Process State Management
$ pm2 start app.js --name="api" # Start application and name it "api"
$ pm2 start app.js -- -a 34     # Start app and pass option "-a 34" as argument
$ pm2 start app.js --watch      # Restart application on file change
$ pm2 start script.sh           # Start bash script
$ pm2 start app.json            # Start all applications declared in app.json
$ pm2 reset [app-name]          # Reset all counters
$ pm2 stop all                  # Stop all apps
$ pm2 stop 0                    # Stop process with id 0
$ pm2 restart all               # Restart all apps
$ pm2 gracefulReload all        # Gracefully reload all apps in cluster mode
$ pm2 delete all                # Kill and delete all apps
$ pm2 delete 0                  # Delete app with id 0

# Startup/Boot management
$ pm2 startup                   # Detect init system, generate and configure pm2 boot on startup
$ pm2 save                      # Save current process list
$ pm2 resurrect                 # Restore previously saved processes
$ pm2 unstartup                 # Disable and remove startup system

$ pm2 update                    # Save processes, kill PM2 and restore processes
$ pm2 init                      # Generate a sample js configuration file

# Deployment
$ pm2 deploy app.json prod setup    # Setup "prod" remote server
$ pm2 deploy app.json prod          # Update "prod" remote server
$ pm2 deploy app.json prod revert 2 # Revert "prod" remote server by 2

# Module system
$ pm2 module:generate [name]    # Generate sample module with name [name]
$ pm2 install pm2-logrotate     # Install module (here a log rotation system)
$ pm2 uninstall pm2-logrotate   # Uninstall module
$ pm2 publish                   # Increment version, git push and npm publish

的更多信息 另请查看https://github.com/Unitech/pm2/tree/master/examples">示例文件夹 以发现所有功能。

Updating PM2

# Install latest PM2 version
$ npm install pm2@latest -g
# Save process list, exit old PM2 & restore all processes
$ pm2 update

PM2 更新是无缝的

Module system

PM2 嵌入了一个简单而强大的模块系统。 安装模块很简单:

$ pm2 install <module_name>

这里有一些 PM2 兼容模块(由 PM2 管理的独立 Node.js 应用程序):

pm2- logrotate 自动轮换 PM2 和应用程序管理的日志
pm2-webshel​​l 在浏览器中公开功能齐全的终端
pm2-server-monit 监控您的服务器运行状况

编写自己的模块

Keymetrics monitoring

Keymetrics Dashboard

如果您使用 PM2 管理您的 NodeJS 应用程序,Keymetrics 可以轻松地跨服务器监控和管理应用程序。 随时尝试:

发现 PM2 的监控仪表板

提前致谢,我们希望您喜欢 PM2!

More about PM2

CHANGELOG

变更日志

Contributors

Contributors

License

PM2 根据 GNU Affero 通用公共许可证 3.0 (AGPL 3.0) 的条款提供。 我们可以提供其他许可证,以获取更多信息联系销售人员

GA


pm2 logo

P(rocess) M(anager) 2
Runtime Edition

npm version NPM Downloads Build Status


PM2 is a General Purpose Process Manager and a Production Runtime for Node.js apps with a built-in Load Balancer.

Key features:

  • Simple and efficient process management (start/stop/restart/delete/show/monit)
  • Keep your application ALWAYS ONLINE with auto restarts and init system script generation
  • Clusterize Node.js Applications without code change to increase performance and reliability
  • Hot Reload Node.js Applications without extra configuration

Starting an application in production mode is as easy as:

$ pm2 start app.js

PM2 is constantly assailed by more than 1800 tests.

Official website: http://pm2.keymetrics.io/

Works on Linux (stable) & macOS (stable) & Windows (stable). All Node.js versions are supported starting Node.js 0.12.

NPM

Installing PM2

$ npm install pm2 -g

npm is a builtin CLI when you install Node.js - Installing Node.js with NVM

Start an application

You can start any application (Node.js, Python, Ruby, binaries in $PATH…) like that:

$ pm2 start app.js

Your app is now daemonized, monitored and kept alive forever.

More about Process Management

Managing a Process

Once applications are started you can manage them easily:

Process listing

To list all running processes:

$ pm2 list

Managing processes is straightforward:

$ pm2 stop     <app_name|id|'all'|json_conf>
$ pm2 restart  <app_name|id|'all'|json_conf>
$ pm2 delete   <app_name|id|'all'|json_conf>

To have more details on a specific process:

$ pm2 describe <id|app_name>

To monitor logs, custom metrics, process information:

$ pm2 monit

More about Process Management

Cluster Mode: Node.js Load Balancing & Hot Reload

The Cluster mode is a special mode when starting a Node.js application, it starts multiple processes and load-balance HTTP/TCP/UDP queries between them. This increase overall performance (by a factor of x10 on 16 cores machines) and reliability (faster socket re-balancing in case of unhandled errors).

Starting a Node.js application in cluster mode that will leverage all CPUs available:

$ pm2 start api.js -i <processes>

<processes> can be 'max', -1 (all cpu minus 1) or a specified number of instances to start.

Hot Reload

Hot Reload allows to update an application without any downtime:

$ pm2 reload all

Seamlessly supported by all major Node.js frameworks and any Node.js applications without any code change:

Framework supported

More informations about how PM2 make clustering easy

Container Support

With the drop-in replacement command for node, called pm2-runtime, run your Node.js application in a proper production environment. We also offer an officialy supported Docker image.

Using it is seamless:

FROM keymetrics/pm2:latest-alpine
[...]
CMD [ "pm2-runtime", "npm", "--", "start" ]

Read More about the dedicated integration

Terminal Based Monitoring

Monit

Monitor all processes launched straight from the command line:

$ pm2 monit

Monitor PM2 and Applications with our SaaS

Once you deploy your application in production, you can monitor, debug and profile it externally with our SaaS Monitoring.

To start monitoring applications from the terminal:

$ pm2 register

More about PM2 Monitoring

Expose Custom Metrics

To get more insights on how your application behave, plug custom metrics inside your code and monitor them with the pm2 monit command:

In your project install pmx:

$ npm install pmx --save

Then plug a custom metric:

var Probe = require('pmx').probe();

var counter = 1;

var metric = Probe.metric({
  name    : 'Counter',
  value   : function() {
    return counter;
  }
});

setInterval(function() {
  counter++;
}, 1000);

Then to see the metric type from in the terminal:

$ pm2 monitor

Metric, Counter, Histogram and Meters are available

Log facilities

Monit

Displaying logs of a specified process or all processes, in real time is easy:

$ pm2 logs ['all'|app_name|app_id] [--json] [--format] [--raw]

Standard, Raw, JSON and formated output are available.

Examples:

$ pm2 logs APP-NAME       # Display APP-NAME logs
$ pm2 logs --json         # JSON output
$ pm2 logs --format       # Formated output

$ pm2 flush               # Flush all logs
$ pm2 reloadLogs          # Reload all logs

More about log management

Startup script generation

PM2 can generates and configure a startup script to keep PM2 and your processes alive at every server restart.

Supports init systems like: systemd (Ubuntu 16, CentOS, Arch), upstart (Ubuntu 14/12), launchd (MacOSx, Darwin), rc.d (FreeBSD).

# Auto detect init system + generate and setup PM2 boot at server startup
$ pm2 startup

# Manually specify the startup system
# Can be: systemd, upstart, launchd, rcd
$ pm2 startup [platform]

# Disable and remove PM2 boot at server startup
$ pm2 unstartup

To save/freeze a process list on reboot:

$ pm2 save

More about startup scripts

Commands Cheatsheet

# General
$ npm install pm2 -g            # Install PM2
$ pm2 start app.js              # Start, Daemonize and auto-restart application (Node)
$ pm2 start app.py              # Start, Daemonize and auto-restart application (Python)
$ pm2 start npm -- start        # Start, Daemonize and auto-restart Node application

# Cluster Mode (Node.js only)
$ pm2 start app.js -i 4         # Start 4 instances of application in cluster mode
                                # it will load balance network queries to each app
$ pm2 reload all                # Zero Second Downtime Reload
$ pm2 scale [app-name] 10       # Scale Cluster app to 10 process

# Process Monitoring
$ pm2 list                      # List all processes started with PM2
$ pm2 list --sort=<field>       # Sort all processes started with PM2
$ pm2 monit                     # Display memory and cpu usage of each app
$ pm2 show [app-name]           # Show all information about application

# Log management
$ pm2 logs                      # Display logs of all apps
$ pm2 logs [app-name]           # Display logs for a specific app
$ pm2 logs --json               # Logs in JSON format
$ pm2 flush
$ pm2 reloadLogs

# Process State Management
$ pm2 start app.js --name="api" # Start application and name it "api"
$ pm2 start app.js -- -a 34     # Start app and pass option "-a 34" as argument
$ pm2 start app.js --watch      # Restart application on file change
$ pm2 start script.sh           # Start bash script
$ pm2 start app.json            # Start all applications declared in app.json
$ pm2 reset [app-name]          # Reset all counters
$ pm2 stop all                  # Stop all apps
$ pm2 stop 0                    # Stop process with id 0
$ pm2 restart all               # Restart all apps
$ pm2 gracefulReload all        # Gracefully reload all apps in cluster mode
$ pm2 delete all                # Kill and delete all apps
$ pm2 delete 0                  # Delete app with id 0

# Startup/Boot management
$ pm2 startup                   # Detect init system, generate and configure pm2 boot on startup
$ pm2 save                      # Save current process list
$ pm2 resurrect                 # Restore previously saved processes
$ pm2 unstartup                 # Disable and remove startup system

$ pm2 update                    # Save processes, kill PM2 and restore processes
$ pm2 init                      # Generate a sample js configuration file

# Deployment
$ pm2 deploy app.json prod setup    # Setup "prod" remote server
$ pm2 deploy app.json prod          # Update "prod" remote server
$ pm2 deploy app.json prod revert 2 # Revert "prod" remote server by 2

# Module system
$ pm2 module:generate [name]    # Generate sample module with name [name]
$ pm2 install pm2-logrotate     # Install module (here a log rotation system)
$ pm2 uninstall pm2-logrotate   # Uninstall module
$ pm2 publish                   # Increment version, git push and npm publish

Also check out the example folder to discover all features.

Updating PM2

# Install latest PM2 version
$ npm install pm2@latest -g
# Save process list, exit old PM2 & restore all processes
$ pm2 update

PM2 updates are seamless

Module system

PM2 embeds a simple and powerful module system. Installing a module is straightforward:

$ pm2 install <module_name>

Here are some PM2 compatible modules (standalone Node.js applications managed by PM2):

pm2-logrotate auto rotate logs of PM2 and applications managed
pm2-webshell expose a fully capable terminal in browsers
pm2-server-monit monitor your server health

Writing your own module

Keymetrics monitoring

Keymetrics Dashboard

If you manage your NodeJS app with PM2, Keymetrics makes it easy to monitor and manage apps across servers. Feel free to try it:

Discover the monitoring dashboard for PM2

Thanks in advance and we hope that you like PM2!

More about PM2

CHANGELOG

CHANGELOG

Contributors

Contributors

License

PM2 is made available under the terms of the GNU Affero General Public License 3.0 (AGPL 3.0). We can deliver other licenses, for more informations contact sales.

GA

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文