pm2 --max-restarts limit not working and continuous restarting crash host system
Asked Answered
B

1

19

I tried pm2 to restrict restart limit with --max-restarts but it's not working and also tired min_uptime

sudo pm2 start server.js --max-restarts=5 

And I also tried with yml file

apps:
  - name: node-mt
    script: server-socket.js
    watch: true
    max_restarts: 5
    min_uptime: 5000

But it's not limiting restart of the application.

If pm2 crashes regularly it crashed the host system and memory usage reached from 300mb to 800mb.

Its normal state when app running.

enter image description here

When an application crashes. Then graph goes very high.

I need to stop max restart to avoid crashing host due to high usage of memory. I don't want to restrict memory usage flag. enter image description here

Bracelet answered 9/3, 2018 at 13:46 Comment(1)
I had watch files on so below dint work. See this answer - https://mcmap.net/q/667819/-how-do-i-limit-the-number-of-auto-restarts-on-pm2Patrimony
T
17

PM2 max_restarts ane min_uptime works perfectly fine. You need to understand the analogy of both.

As per documentation

number of consecutive unstable restarts (less than 1sec(default) interval or custom time via min_uptime) before your app is considered errored and stop being restarted

This means that if your min_uptime is 5000 and max_restarts is 5 then your app will be considered errored if the app is crashed and restarted 5 time in less than 5000ms. If it restarts 4 times in 5 second than it will not consider it as errored and continue restarting it.

If your app keeps restarting with this config that means your app is not restarting 5 times in 5 second. The possible solution is give a relatively high number in min_uptime like an hour or so for your case or you can find it by manual test.

I have a good time understanding this when I first encountered it with my node cron app and created following demo.

app.js

setTimeout(function () {
  console.log('killed');
  process.exit(1)
}, 100);

ecosystem.config.json

{
  "apps" : [{
    "name"         : "api",
    "script"       : "./app.js",
    "max_restarts" : 3,
    "min_uptime"   : 300
  }]
}

This will kill your process but if you change the timeout to 130+(I don't know why but it works for the values less than 130 as may be ms pricision and not considering config till 1st restart) then it won't work. It will start restarting the app.

PM2 documnetation

P.S.

min_uptime can be given in string too.

enter image description here

Tahr answered 5/4, 2018 at 7:50 Comment(6)
Thanks for the response let me try itBracelet
If you go through impmention of pm2 there is only tag --max-restart in pm2 github project not any implementation there is only in readme you can check github.com/Unitech/pm2Bracelet
@Bracelet haev you tried running manually and see when app crashes. As this works pretty fine. It's just your restart time and crash times are mismatching. You can run the example I have provided to understand the processTahr
can we provide a running example wit express app plsBracelet
You can create one by throwing an unhandled error after express initializes with settimeout and monitor the pm2 logs.Tahr
lets discuss in chat i added uBracelet

© 2022 - 2024 — McMap. All rights reserved.