shows how to enable features in your pm2 config file that allow you to prevent runaway apps from bringing your server down. Setting max memory used, number of running processes allowed and maximum restart attempts are all covered.

{
"apps": [{
"name": "App1",
"script": "app1/server.js",
"log_file": "/var/log/pm2/app1.log",
"error_file": "/var/log/pm2/app1-err.log",
"watch": true,
"ignore_watch": ["node_modules"],
"env": {
"NODE_ENV": "production"
},
"instances": 0,
"max_memory_restart": "16G",
"max_restarts": 10
},{
"name": "App2",
"script": "app2/server.js",
"log_file": "/var/log/pm2/app2.log",
"error_file": "/var/log/pm2/app2-err.log"
}
]
}
instances

There are three other options I want to cover real quickly before we wrap this up, and they're done inside the config file again.

The first one I want to cover is the instances object. What instances will do is spin up the number of processes defined here. If we set this to zero, PM2 is going to spin up one process for every CPU you have in your system.

If you want a little finer control of that, you can put in two, four, six, seven, whatever number you'd like to see as far as the number of running processes. The reason the instances option is so cool is because it allows you to do rolling restarts, and graceful reloads of your app.

In the event that you deploy code and issue a reload command to PM2, what's going to happen is it's not going to kill any active connections.

As those connections close or go away, it will stop them and bring up new processes that contain your new code. What essentially happens there is that anyone who's currently using your website during that reload command won't be disconnected.

Their session will stay, and they'll be allowed to finish whatever operation they were on without having their process killed. This creates a much more professional experience for your users.

max_memory_restart

Another cool option that may be handy for you in times of desperate need is the max memory restart. You can use this...let's say that you've been profiling your note app, and you've noticed that if you get above eight gig of memory utilization, your app starts behaving very sporadic and is prone to crash. What you can actually tell PM2 to do is just when you get to eight gig of memory utilization, to go ahead and restart your app.

Clearly, this isn't something that you want to depend on, or it doesn't...you don't want this to be your everyday running configuration. If you've got a bug that you just haven't been able to find, and you've got to keep your site up for your customers, this is going to help you out.

max_restart

The last one we'll cover is max restarts, which is probably a good one to use in combination with max memory restart. What this will do is tell PM2 to only restart your app the number of times listed here, for our example, 10 times.

After 10 times, if your app crashes, PM2 is going to bring that down or it's going to leave it in erred state and it won't attempt to restart it until you stop and restart the entire PM2 process.

This is just like a safety valve, and it's going to keep a rogue app on your system from consuming all your system resources by continually restarting. When it hits that max number, PM2 will just let it die and save your system until you can come in and manually intervene.

 

[Whole Web] [Node.js, PM2] Controlling runaway apps using pm2的更多相关文章

  1. [Whole Web, Node.js, PM2] Configuring PM2 for Node applications

    In this lesson, you will learn how to configure node apps using pm2 and a json config file. Let's sa ...

  2. Node.js~在linux上的部署~pm2管理工具的使用

    之前写了两篇关于在linux上部署nodejs的文章,大家如果没有基础可以先看前两篇<Node.js~在linux上的部署>,<Node.js~在linux上的部署~外网不能访问no ...

  3. [Whole Web] [Node.js] [Browserify] [Grunt] Automation task with grunt-browserify & grunt-contrib-watch

    What we want is when the server side Node.js files have been changed, we want to use browserify to b ...

  4. [Whole Web, Node.js PM2] Loggin with PM2

    Add config for app's log and error log for PM2. { "apps": [{ "name": "App1& ...

  5. [Whole Web, Node.js, PM2] Restarting your node.js app on code change using pm2

    Aadd watch to the config.json file: { "apps": [{ "name": "App1", " ...

  6. [Whole Web] [Node.js] Using npm run to launch local scripts

    npm run allows you to configure scripts inside of your package.json file which can access locally in ...

  7. Node.js 安装配置

    1.安装常用工具: [root@em-nodejs /]# yum -y install vim wget xz 2.下载Node.js二进制安装包: [root@em-nodejs /]# wget ...

  8. 前端(Node.js)(1)-- 初识Node.js

    1.认识 Node.js 诞生.发展.应用现状.生态圈等方面 1.1. 2008年 RyanDahl的目标是创建一个易扩展.适用于现代Web应用通信的服务器平台 1.2.国内外的应用情况 Linked ...

  9. node.js+react全栈实践

    利用业余时间写了个简单的项目,使用react+node.js做的一个全栈实践项目,前端参考了[React-Admin-Starter](https://github.com/veryStarters/ ...

随机推荐

  1. java中的CAS

    转自:http://www.blogjava.net/mstar/archive/2013/04/24/398351.html Atomic 从JDK5开始, java.util.concurrent ...

  2. Android USB Host与HID通讯 (二)

    不好意思,从上一篇到现在确实比较忙,中间又外出了一段时间,虽然也上LOFTER,或者看到一些朋友QQ上加我,给我发信息询问,有些看到了有些可能没看到,偶尔回复了一两个,也不咋的详细,在此我想说,一方面 ...

  3. 【大话QT之十】实现FTP断点续传(需要设置ftp服务器为“PASV”被动接收方式)

    应用需求: 网盘开发工作逐步进入各部分的整合阶段,当用户在客户端修改或新增加一个文件时,该文件要同步上传到服务器端对应的用户目录下,因此针对数据传输(即:上传.下载)这一块现在既定了三种传输方式,即: ...

  4. C# Http请求(GET/HTTP/HTTPS)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  5. 同一客户代码下一个CPN对应多个FG会出现CPN描述一样的问题

    Q&A : D2E01客户下已经存在CPN: TP1062/A 对应FG: P5M2812512AFHHF0 直接在客户代码下无法新建,只能在交叉信息新建CPN: TP1062/A, 但无法修 ...

  6. CodeForces_#354_Div.2_2016.5.25(A+B+C)

    A 描述:给出一串数,可以互换任意两个数的位置一次,求最大的数和最小的数的最大距离. 分析:找到最大的数和最小的数的位置,求右边的数到左端点的距离和左边的数到右端点的距离. #include < ...

  7. ☀【css】透明

    让IE浏览器支持RGBA颜色http://www.hujuntao.com/web/css/ie-browser-support-for-rgba-color.html 透明纯色背景 backgrou ...

  8. Xcode6 运行程序后,右侧Debug区域的Memory显示空白解决方法

    http://chenyh-blog.com/%E8%9B%8B%E7%96%BC%E7%9A%84%E5%86%85%E5%AD%98-%E7%AC%AC%E4%B8%89%E7%AF%87-sdw ...

  9. 使用PowerShell脚本部署定时器到MOSS2010

    转:http://www.77site.com/tech/1087042010072906074113_2012050808152911.html 第一章 前言 在此次练习中,您将了解到如何使用Pow ...

  10. asp.net mvc ChildActionOnly 和ActionName的用法

    ChildActionOnly的目的主要就是让这个Action不通过直接在地址栏输入地址来访问,而是需要通过RenderAction来调用它. <a href="javascript: ...