码迷,mamicode.com
首页 > 其他好文 > 详细

pm2中文文档

时间:2017-02-21 00:25:37      阅读:7762      评论:0      收藏:0      [点我收藏+]

标签:重置   pass   type   pps   daemonize   使用   ini   --   macos   

技术分享
                                                                                             

www.zhentaoo.com


PM2是Node.js应用程序的生产流程管理器,内置负载均衡。它可以帮助您保持Node应用程序永久活动,重起这些node应用程序也不需要停机,并简化常见的系统管理任务。

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

在生产模式下启动应用程序非常简单
$ pm2 start app.js

 

安装 PM2

$ npm install pm2 -g

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

 

启动一个应用程序

$ pm2 start app.js

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

More about Process Management

 

更新 PM2

# 安装最新的pm2
$ npm install pm2 -g
# Save process list, exit old PM2 & restore all processes
$ pm2 update

PM2 updates are seamless

 

主要功能

常用命令一览

# 普通General
$ npm install pm2 -g # 安装 PM2 $ pm2 start app.js # 启动,守护进程,自动重启应用程序 Start, Daemonize and auto-restart application (Node) $ pm2 start app.py # 启动,守护进程,自动重启python应用程序 Start, Daemonize and auto-restart application (Python) $ pm2 start npm -- start # 启动,守护进程,自动重启node应用程序 Start, Daemonize and auto-restart Node application
# 集群模式 (只支持node进程) Cluster Mode (Node.js only) $ pm2 start app.js -i 4 # 在集群模式下,启动4个应用程序实例 Start 4 instances of application in cluster mode # 同时,将网络请求,负载均衡到每个应用实例 it will load balance network queries to each app $ pm2 reload all # 0秒重启所有应用 Zero Second Downtime Reload $ pm2 scale [app-name] 10 # 将应用进程调整到10 Scale Cluster app to 10 process # 进程监控 Scale Cluster app to 10 process $ pm2 list # 列出所有用PM2启动的进程 List all processes started with PM2 $ pm2 monit # 显示每个应用占用的cpu和内存 Display memory and cpu usage of each app $ pm2 show [app-name] # 显示某个进程的所有信息 Show all informations about application # 日志管理 Log management $ pm2 logs # 显示所有应用的日志 Display logs of all apps $ pm2 logs [app-name] # 显示某个应用的日志 Display logs for a specific app $ pm2 logs --json # json化日志 Logs in JSON format $ pm2 flush $ pm2 reloadLogs # 进程状态管理 Process State Management $ pm2 start app.js --name="api" # 启动一个应用并命名为api。 Start application and name it "api" $ pm2 start app.js -- -a 34 # 启动一个应用,并传递“-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 # 启动一个bash脚本。Start bash script $ pm2 start app.json # 启动在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 # 停止id 为0的应用。Stop process with id 0 $ pm2 restart all # 重启所有应用。Restart all apps $ pm2 gracefulReload all # 在集群模式下,平稳的重加载所有应用。Graceful reload all apps in cluster mode $ pm2 delete all # 杀掉所有应用。Kill and delete all apps $ pm2 delete 0 # 杀掉id为0的进程。Delete app with id 0 # 启动/引导管理 Startup/Boot management $ pm2 startup # 检测init系统,在启动时生成和配置pm2。Detect init system, generate and configure pm2 boot on startup $ pm2 save # 保存当前进程列表。Save current process list $ pm2 resurrect # 恢复以前保存的进程。Restore previously save processes $ pm2 unstartup # 停用和删除启动系统。Disable and remove startup system $ pm2 update # 保存进程,终止PM2并恢复进程。Save processes, kill PM2 and restore processes $ pm2 generate # 生成样本json配置文件。Generate a sample json 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 # 将“生产环境”远程服务器恢复2。Revert "prod" remote server by 2 # 模块系统 Module system $ pm2 module:generate [name] # 生成名称为[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 # 增量版本,git push和npm发布。Increment version, git push and npm publish

 

进程管理

一旦应用程序启动,您可以轻松地列出和管理它们:

技术分享

 

列出正在运行的应用程序

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

要确保它在json_conf使用中重新评估环境变量 To make sure it re-evaluates enviroment variables in your json_conf use

$ pm2 restart <json_conf>

要了解特定流程的更多详细信息

$ pm2 describe <id|app_name>

 

More about Process Management

Load Balancing & Zero second Downtime Reload

When an application is started with the -i option, the Cluster Mode is enabled.

The Cluster Mode start and automatically load balance HTTP/TCP/UDP between each instance. This allows to increase overall performance depending to the number of CPUs availabe.

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

技术分享

 

Main commands:

主要命令:

$ pm2 start app.js -i max  # 启用负载平衡,启动‘max‘实例(cpu nb)。Enable load-balancer and start ‘max‘ instances (cpu nb)

$ pm2 reload all           # 0秒重启。Zero second dowtime reload

$ pm2 scale <app_name> <instance_number> # 增加/减少进程号。Increase / Decrease process number

More informations about how PM2 make clustering easy

 

CPU/内存 监控

技术分享

 

监控所有启动的进程:Monitoring all processes launched:

$ pm2 monit

 

日志设施  Log facilities

技术分享

 

实时显示指定进程或所有进程的日志。提供标准,原始,JSON和格式输出。

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

例子:

$ 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可以生成和配置启动脚本,以在每次服务器重新启动时保持PM2和您的进程活动。
支持init系统,如:systemd(Ubuntu 16,CentOS,Arch),upstart(Ubuntu 14/12),launchd(MacOSx,Darwin),rc.d(FreeBSD)。
# 自动检测init系统+在服务器启动时生成和设置PM2引导。 Auto detect init system + generate and setup PM2 boot at server startup
$ pm2 startup

# 手动指定启动系统. Manually specify the startup system
# 可以是:systemd,upstart,launchd,rcd。
$ pm2 startup [platform]

# 在服务器启动时禁用和删除PM2引导。Disable and remove PM2 boot at server startup
$ pm2 unstartup

 

在重新启动时保存/冻结进程列表:To save/freeze a process list on reboot:

$ pm2 save

 

More about startup scripts

模块系统 Module system

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

$ pm2 install <module_name>

下面是一些PM2兼容模块(由PM2管理的独立Node.js应用程序):Here are some PM2 compatible modules (standalone Node.js applications managed by PM2):

pm2-logrotate 自动轮换PM2的日志和管理的应用程序 (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

技术分享

 

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

Discover the monitoring dashboard for PM2

感谢,我们希望你喜欢PM2!

 

 

translate by 青菜叶子(zhentaoo)

 

pm2中文文档

标签:重置   pass   type   pps   daemonize   使用   ini   --   macos   

原文地址:http://www.cnblogs.com/zhentaoo/p/6421977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!