[Whole Web] [Node.js, PM2] Controlling runaway apps using pm2
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的更多相关文章
- [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 ...
- Node.js~在linux上的部署~pm2管理工具的使用
之前写了两篇关于在linux上部署nodejs的文章,大家如果没有基础可以先看前两篇<Node.js~在linux上的部署>,<Node.js~在linux上的部署~外网不能访问no ...
- [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 ...
- [Whole Web, Node.js PM2] Loggin with PM2
Add config for app's log and error log for PM2. { "apps": [{ "name": "App1& ...
- [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", " ...
- [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 ...
- Node.js 安装配置
1.安装常用工具: [root@em-nodejs /]# yum -y install vim wget xz 2.下载Node.js二进制安装包: [root@em-nodejs /]# wget ...
- 前端(Node.js)(1)-- 初识Node.js
1.认识 Node.js 诞生.发展.应用现状.生态圈等方面 1.1. 2008年 RyanDahl的目标是创建一个易扩展.适用于现代Web应用通信的服务器平台 1.2.国内外的应用情况 Linked ...
- node.js+react全栈实践
利用业余时间写了个简单的项目,使用react+node.js做的一个全栈实践项目,前端参考了[React-Admin-Starter](https://github.com/veryStarters/ ...
随机推荐
- [JAVA]HDU 4919 Exclusive or
题意很简单, 就是给个n, 算下面这个式子的值. $\sum\limits_{i=1}^{n-1} i \otimes (n-i)$ 重点是n的范围:2≤n<10500 比赛的时候 OEIS一下 ...
- 手势识别官方教程(2)识别常见手势用GestureDetector+手势回调接口/手势抽象类
简介 GestureDetector识别手势. GestureDetector.OnGestureListener是识别手势后的回调接口.GestureDetector.SimpleOnGesture ...
- 【HDOJ】1263 水果
hash,使用stl map ac.学了find_if等强大的东西,第一次使用stl模板. #include <iostream> #include <cstdio> #inc ...
- GridView導出Excel 解決亂碼問題
Response.Clear(); Response.Charset = "gb2312"; Response.Buffer = true; Response.AddHeader( ...
- 以编程方式使用 Word 中的内置对话框
使用 Microsoft Office Word 时,有时需要显示用户输入对话框.虽然可以创建自己的对话框,您也许还希望采用使用 Word 中内置对话框的方法,这些对话框在Application 对象 ...
- 从零开始学习jQuery (六) AJAX快餐
一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案, 即使你会使用jQuery也能在阅读中发现些许秘籍. 本篇文章讲解如何使用jQuery方便快捷的实现A ...
- RESTLET开发实例(三)基于spring的REST服务
http://www.lifeba.org/arch/restlet_spring_3.html 前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务.这里将 ...
- [转]oracle性能调优之--Oracle 10g AWR 配置
一.ASH和AWR的故事 1.1 关于ASH 我们都知道,用户在ORACLE数据库中执行操作时,必然要创建相应的连接和会话,其中,所有当前的会话信息都保存在动态性能视图V$SESSION中,通过该视图 ...
- SQL经典题-实战
Student(S#,Sname,Sage,Ssex) 学生表 S#:学号:Sname:学生姓名:Sage:学生年龄:Ssex:学生性别 Course(C#,Cname,T#) 课程表 ...
- linux内核中分配4M以上大内存的方法
在内核中, kmalloc能够分配的最大连续内存为2的(MAX_ORDER-1)次方个page(参见alloc_pages函数, "if (unlikely(order >= ...