Skip to content

Instantly share code, notes, and snippets.

@mudkipme
Last active December 18, 2015 06:08
Show Gist options
  • Save mudkipme/5737462 to your computer and use it in GitHub Desktop.
Save mudkipme/5737462 to your computer and use it in GitHub Desktop.
Monitors mutiple services on 52Poké server.
var exec = require('child_process').exec,
fs = require('fs'),
async = require('async');
var services = [ 'php-fpm', 'nginx', 'mysqld', ['mongod', 'mongodb'], ['redis-server', 'redis'] ];
var logFile = '/home/52poke/logs/monitor.log';
var actions = [];
var addLog = function(str){
fs.appendFileSync(logFile, new Date().toString()+ str + '\n');
};
services.forEach(function(service){
var serv = service;
if (!Array.isArray(service)) {
serv = [service, service];
}
actions.push(function(callback){
exec('pgrep ' + serv[0], function(err, stdout, stderr){
if (!stdout || !stdout.trim()) {
addLog(serv[1] + ' is not working!!! Restarting...');
exec('systemctl restart ' + serv[1], function(){
callback();
});
} else {
callback();
}
});
});
});
var monitor = function(){
async.series(actions, function(){
setTimeout(monitor, 10000);
});
};
monitor();
fs.appendFileSync(logFile, new Date().toString()+' Monitor started.\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment