Setting crontab from database

pull/1/head
Suresh Alse 2015-06-12 01:15:04 +05:30
parent 4700ddb4a5
commit e5b466aa50
1 changed files with 16 additions and 2 deletions

View File

@ -3,6 +3,8 @@ var Datastore = require('nedb');
var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' }); var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' });
db.loadDatabase(function (err) { db.loadDatabase(function (err) {
}); });
var exec = require('child_process').exec;
var fs = require('fs');
crontab = function(command, schedule, stopped){ crontab = function(command, schedule, stopped){
var data = {}; var data = {};
@ -19,11 +21,23 @@ exports.create_new = function(command, schedule){
} }
exports.crontabs = function(callback){ exports.crontabs = function(callback){
var finished = false;
db.find({}, function(err, docs){ db.find({}, function(err, docs){
finished = true;
callback(docs); callback(docs);
}); });
} }
exports.set_crontab = function(){
exports.crontabs( function(tabs){
var crontab_string = "";
tabs.forEach(function(tab){
if(!tab.stopped){
crontab_string += tab.schedule + " " + tab.command + "\n";
}
});
fs.writeFile("/tmp/crontab", crontab_string, function(err) {
//couldnt write
});
exec("crontab /tmp/crontab");
});
}