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.
Starting an application in production mode is as easy as:
$ pm2 start app.js
PM2 is constantly assailed by more than 1000 tests.
Official website: http://pm2.keymetrics.io/
Works on Linux (stable) & MacOSx (stable) & Windows (stable).
Install PM2
$ npm install pm2 -g
# Or install the V2 release candidate
$ npm install pm2@next -g
npm is a builtin CLI when you install Node.js - Installing Node.js with NVM
Read more about the V2 - Quantica Ultimatum
Start an application
$ pm2 start app.js
Your app is now put in background, monitored and kept alive forever.
Update PM2
# Install latest pm2 version
$ npm install pm2 -g
# Save process list, exit old PM2 & restore all processes
$ pm2 update
PM2 updates are seamless
Main features
Commands overview
$ npm install pm2 -g # Install PM2
$ pm2 start app.js # Start, Daemonize and auto restart application
$ pm2 start app.js -i 4 # Start 4 instances of application in cluster mode
# it will load balance network queries to each app
$ pm2 start app.js --name="api" # Start application and name it "api"
$ pm2 start app.js --watch # Restart application on file change
$ pm2 start script.sh # Start bash script
$ pm2 list # List all processes started with PM2
$ pm2 monit # Display memory and cpu usage of each app
$ pm2 show [app-name] # Show all informations about application
$ pm2 logs # Display logs of all apps
$ pm2 logs [app-name] # Display logs for a specific app
$ pm2 flush
$ pm2 stop all # Stop all apps
$ pm2 stop 0 # Stop process with id 0
$ pm2 restart all # Restart all apps
$ pm2 reload all # Reload all apps in cluster mode
$ pm2 gracefulReload all # Graceful reload all apps in cluster mode
$ pm2 delete all # Kill and delete all apps
$ pm2 delete 0 # Delete app with id 0
$ pm2 scale api 10 # Scale app with name api to 10 instances
$ pm2 reset [app-name] # Reset number of restart for [app-name]
$ pm2 startup # Generate a startup script to respawn PM2 on boot
$ pm2 save # Save current process list
$ pm2 resurrect # Restore previously save processes
$ pm2 update # Save processes, kill PM2 and restore processes
$ pm2 generate # Generate a sample json configuration file
$ 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
$ 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
Different ways of starting processes
$ pm2 start app.js --watch # Restart application on file change
$ pm2 start script.sh # Start bash script
$ pm2 start app.js -- -a 34 # Start app and pass option -a 34
$ pm2 start app.json # Start all applications declared in app.json
$ pm2 start my-python-script.py --interpreter python
Process management
Once apps are started you can list and manage them easily:
Listing all running processes:
$ pm2 list
Managing your 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>
Load balancing / 0s reload downtime
When an app is started with the -i option, the cluster mode is enabled.
Supported by all major Node.js frameworks and any Node.js applications
Warning: If you want to use the embedded load balancer (cluster mode), we recommend the use of
node#0.12.0+
or node#0.11.16+
. We do not support node#0.10.*
's cluster module anymore.
With the cluster mode, PM2 enables load balancing between multiple application to use all CPUs available in a server. Each HTTP/TCP/UDP request will be forwarded to one specific process at a time.
$ pm2 start app.js -i 0 # Enable load-balancer and cluster features
$ pm2 reload all # Reload all apps in 0s manner
$ pm2 scale <app_name> <instance_number> # Increase / Decrease process number
CPU / Memory Monitoring
Monitoring all processes launched:
$ pm2 monit
Log facilities
Displaying logs of a specified process or all processes, in real time:
pm2 logs ['all'|'PM2'|app_name|app_id] [--err|--out] [--lines <n>] [--raw] [--timestamp [format]]
Examples:
$ pm2 logs
$ pm2 logs WEB-API --err
$ pm2 logs all --raw
$ pm2 logs --lines 5
$ pm2 logs --timestamp "HH:mm:ss"
$ pm2 logs WEB-API --lines 0 --timestamp "HH:mm" --out
$ pm2 logs PM2 --timestamp
$ pm2 flush # Clear all the logs
Startup script generation
PM2 can generate and configure a startup script to keep PM2 and your processes alive at every server restart. Execute the startup command only as the user to be running the PM2 daemon.
$ pm2 startup
# auto-detect platform
$ pm2 startup [platform]
# render startup-script for a specific platform, the [platform] could be one of:
# ubuntu|centos|redhat|gentoo|systemd|darwin|amazon
To save a process list just do:
$ pm2 save
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-auto-pull auto pull all applications managed by PM2
pm2-webshell expose a fully capable terminal in browsers
pm2-auto-pull auto pull all applications managed by PM2
Keymetrics monitoring
If you manage your NodeJS app with PM2, Keymetrics makes it easy to monitor and manage apps across servers. Feel free to try it:
Thanks in advance and we hope that you like PM2!
More about PM2
- Watch & Restart
- Application Declaration via JS files
- PM2 API
- Deploying workflow
- PM2 and Heroku/Azure/App Engine
- PM2 auto completion
from https://github.com/Unitech/pm2
----------------------------------------------------
pm2官方文档:http://pm2.keymetrics.io/docs/usage/quick-start/
简单教程
首先需要安装pm2:
npm install -g pm2
运行:
pm2 start app.js
开机自动运行:
pm2 start app.js --watch
开机自启:
pm2 startup
pm2 save
初次安装并运行,会有一个高大上的界面:
直接我们介绍过forever,那么pm2与forever相比较有哪些高大上的功能呢?我们看一下对比表格:
Feature Forever PM2
Keep Alive ✔ ✔
Coffeescript ✔
Log aggregation ✔
API ✔
Terminal monitoring ✔
Clustering ✔
JSON configuration ✔
我们可以很直观的看出,pm2相比较Forever,功能更加强大一些。
查看运行状态
我们可以通过简单的命令查看应用的运行状态:
pm2 list
效果如下:
ANodeBlog应用正在运行,pid为31480,并且占用内存为89.113 MB。
追踪资源运行情况
pm2 monit
会看到应用资源的实时运行情况
查看应用详细部署状态
如果我们想要查看一个应用详细的运行状态,比如ANodeBlog的状态,可以运行:
pm2 describe 3
"3"是指App Id。
结果如下:
查看日志
pm2 logs
系统会打印出详细的logs。
重启应用
pm2 restart appId
停止应用
想要终止应用,只需要运行:
pm2 stop app.js
强健的API
在项目中运行:
pm2 web
然后浏览器访问http://localhost:9615 你会有惊喜!
预定义运行配置文件
我们可以预定义一个配置文件,然后制定运行这个配置文件,比如我们定义一个文件process.json,内容如下:
{
"apps": [
{
"name": "ANodeBlog",
"script": "bin/www",
"watch": "../",
"log_date_format": "YYYY-MM-DD HH:mm Z"
}
]
}
然后可以通过
pm2 start process.json
运行这个App。
总结
常用命令总结如下:
安装pm2
npm install -g pm2
启动应用
pm2 start app.js
列出所有应用
pm2 list
查看资源消耗
pm2 monit
查看某一个应用状态
pm2 describe [app id]
查看所有日志
pm2 logs
重启应用
pm2 restart [app id]
停止应用
pm2 stop [app id]
开启api访问
pm2 web
更多pm2内容请参考官方文档:http://pm2.keymetrics.io/docs/usage/quick-start