Created
April 28, 2017 10:24
-
-
Save yanzhihong23/c9618d672eee0f467af872ea23a9f39c to your computer and use it in GitHub Desktop.
pm2 cpu guard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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!`); | |
} | |
}); | |
} | |
}) | |
}); | |
} | |
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
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.