Watch & Restart
Auto restart apps on file change
当文件被修改时,pm2默认可以自动restart app
$ pm2 start app.js --watch
如果 –watch 启用了,那么stop将不会停止监听:
- pm2 stop 0 不会停止监听
- pm2 stop –watch 0 将停止监听
注意:watch将不会触发任何 graceful action,pm2 kill 和 restart app,不会发送 SIGINT
想要watch指定的路径,请使用一个 js/json app 声明, watch 可以接收字符串或者一个路径数组。默认是false
{
"watch": ["server", "client"],
"ignore_watch" : ["node_modules", "client/img"],
"watch_options": {
"followSymlinks": false
}
}
配置说明:
- wath: 可以是boolean, array, string。默认是false;
- ignore_watch: 可以是array, string, 将会被翻译成 glob 或正则表达式;
- watch_options: 选项说明请参考 chokidar documentation
默认的 chokidar配置如下:
var watch_options = {
persistent : true,
ignoreInitial : true
}
When working with NFS devices you’ll need to set usePolling: true as stated in [this chokidar issue](https://github.com/paulmillr/chokidar/issues/242).
Monitoring - 监控
Monitoring CPU/Memory - 监控cpu/内存
这章里面官方有配图,请到官方地址查看。 PM2给你一个简单的方法来监视你的应用程序的资源使用情况。您可以轻松地从终端监控内存和CPU:$ pm2 monit
Keymetrics monitoring
如果你使用pm2来管理你的node应用,我们推荐你使用 Keymetrics 。它使在服务器上监控和管理应用程序比以往更容易。 [Discover the monitoring dashboard for PM2](https://app.keymetrics.io/#/register)Memory threshold
PM2允许reload(自动回退重启)基于内存限制的应用。请注意,PM2内部线程(检查内存和相关),开始每30秒一次,所以你可能要等待一下当你的进程达到内存阈值后自动restart。- CLI
$ pm2 start big-array.js --max-memory-restart 20M
- JSON
{
"name" : "max_mem",
"script" : "big-array.js",
"max_memory_restart" : "20M"
}
Source map support
如果你使用 BabelJS, Typescript 或 其他 Javascript superset, 你可能已经注意到,当异常发生时,堆栈跟踪都是没有意义的。为了获得有效的信息,需要生成source map files。
官方有图
一旦source map文件生成,PM2 会自动检测并将帮助你检查错误。
Source map
从版本 1.0.2 开始,pm2 支持 javascript source map。
PM2 自动检测JavaScript source map文件如果你start的文件(我们说的app.js)有它的map文件(如app.js.map)。
如果您有不同的布局,可以通过启动应用程序强制源代码映射支持:
- CLI :
$ pm2 start app.js --source-map-support
- Or via JSON file :
{
"apps" : [{
"name" : "my-app",
"script" : "script.js",
"source_map_support": true
}]
}
Inspect exceptions - 检查异常
异常消息被记录到app 的 error log file。
要检查异常日志,你只需要键入:
$ pm2 logs main
使用 keymetrics.io 获取一个干净的列表,以及 new alerts happening 的通知。
Disable source map support
如果想禁用pm2 自动支持 js source map,你可以使用配置 –disable-source-map,或者在json file中配置