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. Lua 笔记

    lua命令: #enter shell lua #excute script file lua xxx.lua lua脚本: #!/usr/local/bin/lua 核心概念: As a exten ...

  2. javaweb学习总结(三十六)——使用JDBC进行批处理

    在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...

  3. 8. Unity异常警告错误处理方法

    一. The AnimationClip 'cube1_anim' used by the Animation component 'Cube1' must be marked as Legacy. ...

  4. Introducing RecyclerView(一)

    RecyclerView 是Android L版本中新添加的一个用来取代ListView的SDK,它的灵活性与可替代性比listview更好.接下来通过一系列的文章讲解如何使用RecyclerView ...

  5. 在电脑上装ubuntu12.04系统,内核文件是那个?

    在电脑上装ubuntu12.04系统,我们能看到的是根文件系统,那么内核文件(zlmage)是那个? ???

  6. Drawable(3)Color State List Resource

    注意 Color State List Resource 与 Color不同,前者是颜色状态表.根据不同状态显示不同颜色,它是State list,里面有多种颜色,后者只是一个颜色. Color St ...

  7. wzplayer2 for windows ActiveX 试用地址

    提供wzplayer2 for windows ActiveX,测试地址:http://www.coolradio.cn/IE.htm 大家使用时候必须允许未签名ActiveX下载和运行,否则将无法正 ...

  8. bzoj3171

    不难发现,每个点出度显然为1,要想整个图形成环,必然每个点的入度也为1: 所以,不难想到将每个点i拆成两个点i1,i2构成二分图, 连边s--->i1 流量为1,费用为0,i2--->t流 ...

  9. bzoj1443

    首先要思考的问题肯定是,什么点可以是ans, 不难想到对图黑白染色,假如一个棋子不管怎么走,都只能走到和它同色的点上时,这就是一个ans 再考虑,每次棋子的移动都是黑白相间的 再考虑,黑白染色是可以构 ...

  10. MySQL配置文件-my.ini

    下面允许我介绍一下MySQL的my.ini配置文件: my.ini是什么? my.ini是MySQL数据库中使用的配置文件,修改这个文件可以达到更新配置的目的. my.ini存放在哪里? my.ini ...