npm系列:pm2文档简单翻译

今天实现了一个用node写了一个小网页放在服务器,为了解决关闭链接后退出node的执行,找了forrver和pm2,权衡之下,选择了pm2。npm上找到了其文档,简单翻译一下,留备后用。
奈何我做为一个英语四级都没有过的学渣,翻译水平实在有限,有疏漏不通之处,往你们谅解。(其实我就当作一个学习英语的方式了)node

文档地址linux

PM2是一个带有负载均衡功能的Node应用的进程管理器。它能够保持Node应用一直后台运行,在不中止的前提下更新,优化了系统任务管理。
启动进程管理十分简单:git

$ pm2 start app.js

PM2通过了超过1000次的测试。
而且可以运行在linux&MacOSx&Windows等操做系统上。github

pm2.png

Install PM2

$ npm install pm2 -g

当你安装了node以后,npm也会被安装--Installing Node.js with NVM
NVM是一个Nodejs的版本控制器。web

启动一个应用

$ npm install pm2 -g

你的程序如今是一个受监控的后台进程,而且会一直运行下去。
More about Process Managementshell

Update PM2

# Install latest pm2 version

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

PM2更新是无缝的npm

主要特色

命令概述json

# General

$ npm install pm2 -g           # 安装 PM2

$ pm2 start app.js             #启动, Daemonize and auto-restart application (Node)

$ pm2 start app.py             # 启动, Daemonize and auto-restart application (Python)

$ pm2 start npm -- start       # 启动, Daemonize and auto-restart Node application

 
# Cluster Mode (Node.js only)
$ pm2 start app.js -i 4         # 启动四个应用程序实例

                               # 会给每一个网络装载网络均衡器

$ pm2 reload all               # 当即重启全部工做进程

$ pm2 scale [app-name] 10       # 使用10个工做进程

 
# Process Monitoring
$ pm2 list                     # 列出全部PM2启动的进程

$ pm2 monit                     # 列出每一个程序的详细状况和CPU使用状况

$ pm2 show [app-name]           # 列出程序的全部信息

 
# Log management
$ pm2 logs                     # 列出应用的日志

$ pm2 logs [app-name]           # 列出某个app的日志

$ pm2 logs --json               # 使用json格式列出日志

$ pm2 flush
$ pm2 reloadLogs
 
# Process State Management
$ pm2 start app.js --name="api" # 启动应用,而且命名‘api’

$ pm2 start app.js -- -a 34     # Start app and pass option "-a 34" as argument
$ pm2 start app.js --watch     # 当文件改变的时候从新启动应用

$ pm2 start script.sh           # 启动bash脚本

$ pm2 start app.json           # 启动全部app.json中声明的程序

$ pm2 reset [app-name]         # Reset all counters

$ pm2 stop all                 # 中止全部应用

$ pm2 stop 0                   # 中止id为0的进程

$ pm2 restart all               # 重启全部应用

$ pm2 gracefulReload all       # Graceful reload all apps in cluster mode
$ pm2 delete all               # 关闭并删除全部应用

$ pm2 delete 0                 # 删除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 save processes
$ pm2 unstartup                 # Disable and remove startup system
 
$ pm2 update                   # Save processes, kill PM2 and restore processes
$ pm2 generate                 # 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 # Revert "prod" remote server by 2
 
# Module system
$ pm2 module:generate [name]   # Generate sample module with name [name]

$ pm2 install pm2-logrotate     # 安装模块 (here a log rotation system)

$ pm2 uninstall pm2-logrotate   # 卸载模块

$ pm2 publish                   # 版本发布, git推送和npm发布

进程管理器

一点进程启动,你能够查看列表而且轻松的管理:api

pm2-list.png

监听全部运行的进程bash

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

More about Process Management

负载平衡和当即重载

当启动应用的时候使用 -i <instance_number>选项,集群模式将会启用。集群模式将会instance_number而后自动加载 HTTP/TCP/UDP 的均衡器。能够根据CPU可得到数目在增长性能。
PM2支持全部的Nodejs框架,而且任何Node.js程序不用修改任何代码即能运行

cluster-support.png

主要命令:

$ pm2 start app.js -i max # Enable load-balancer and start 'max' instances (cpu nb)
$ pm2 reload all           # Zero second dowtime reload
$ pm2 scale <app_name> <instance_number> # Increase / Decrease process number

More informations about how PM2 make clustering easy

CPU/内存监控

pm2-monit.png

监视全部进程:

$ pm2 monit

日志工具

pm2-logs.png

即时地展现详细的进程日志。有 Standard, Raw, JSON和formated
输出可使用:

$ 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

配置启动脚本

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

More about startup scripts

模块系统

PM2内置了一个简单却强大的模块系统,安装模块十分简单:

$ pm2 install <module_name>

这里有一些兼容的模块,可以经过PM2管理Nodejs库:
pm2-logrotate 自动记录PM2和程序的管理日志
pm2-webshell 提供一个终端
pm2-server-monit 监控系统运行
Writing your own module

Keymetrics monitoring

application-demo.png

若是你使用PM2管理Nodejs应用,Keymetrics会使你管理简单,而且能够是跨服务器。
Discover the monitoring dashboard for PM2
十分感谢,但愿您能喜欢PM2

More about PM2

Application Declaration via JS files

Watch & Restart

PM2 API

Deployment workflow

PM2 on Heroku/Azure/App Engine

PM2 auto completion

Using PM2 in ElasticBeanStalk

CHANGELOG

CHANGELOG

Contributors

Contributors

注:

翻译水平较低,并且使用的并不深刻。随着之后理解的加深,再逐渐修改翻译文档。

相关文章
相关标签/搜索