How to limit autorun on pm2?

I have a node server running on pm2 that depends on some external services.

When these servers go down, I start to restart my pm2 application, but this will continue until it shuts down my processor and starts on the server, rebooting up to 50 times per minute.

Is there a way to limit the number of restarts on pm2? There is a way to restart the server when the server reaches a certain level of RAM, so I hope that this function that I ask about exists.

+8
source share
2 answers

You can use a combination of max_restarts and min_uptime to limit restarting the application.

the number of consecutive unstable restarts (less than 1 sec. or user time after min_uptime) before your application is considered to be erroneous and stops restarting

More information on max_restarts and min_uptime is available here.

+7
source

Use the PM2 architecture.config.js file as follows:

 module.exports = { apps : [{ name: "app", script: "./app.js", merge_logs: true, max_restarts": 50, //Here you can define your max restarts instances: "max", max_memory_restart: "200M", env: { NODE_ENV: "development", }, env_production: { NODE_ENV: "production", } }] } 

Start your server by running the command:

 pm2 start ecosystem.config.js //uses variables from 'env' pm2 start ecosystem.config.js --env production //uses variables from 'env_production' 

See the link below for more details:

PM2 Runtime | Guide | Ecosystem file

0
source

Source: https://habr.com/ru/post/1262855/


All Articles