Skip to content

Instantly share code, notes, and snippets.

@yanzhihong23
Created April 28, 2017 10:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanzhihong23/c9618d672eee0f467af872ea23a9f39c to your computer and use it in GitHub Desktop.
Save yanzhihong23/c9618d672eee0f467af872ea23a9f39c to your computer and use it in GitHub Desktop.
pm2 cpu guard
'use strict';
const later = require('later');
const logger = require('./logger')('guard');
const pm2 = require('pm2');
const schedule = later.parse.recur().every(10).second();
// set local timezone
later.date.localTime();
later.setInterval(pm2guard, schedule);
function pm2guard() {
pm2.list((err, processDescriptionList) => {
if(err) {
logger.error(err);
return;
}
processDescriptionList.forEach(item => {
if(item.monit.cpu > 90) {
pm2.restart(item.pm_id, _err => {
if(_err) {
logger.error(_err);
} else {
logger.info(`pm2 restart ${item.pm_id} success!`);
}
});
}
})
});
}
@moneyhan
Copy link

moneyhan commented Nov 1, 2017

What sort of impact does this have on running processes? Running into similar issues where PM2 for a specific service goes out of control and consumes 100% cpu.

@moneyhan
Copy link

moneyhan commented Nov 1, 2017

Also, what manages this script? CRON job or something else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment