diff --git a/.gitignore b/.gitignore index 16b5a70..cea8ed1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.swp node_modules crontabs/*.db +crontabs/logs/*.log diff --git a/app.js b/app.js index 7109d34..d8cd387 100755 --- a/app.js +++ b/app.js @@ -37,7 +37,7 @@ app.get(routes.root, function(req, res) { res.render('index', { routes : JSON.stringify(routes), crontabs : JSON.stringify(docs), - backups : crontab.get_backup_names() + backups : crontab.get_backup_names(), }); }); }) @@ -45,7 +45,7 @@ app.get(routes.root, function(req, res) { app.post(routes.save, function(req, res) { // new job if(req.body._id == -1){ - crontab.create_new(req.body.name, req.body.command, req.body.schedule); + crontab.create_new(req.body.name, req.body.command, req.body.schedule, req.body.logging); } // edit job else{ @@ -132,6 +132,11 @@ app.get(routes.import_crontab, function(req, res) { res.end(); }) +app.get(routes.logger, function(req, res) { + console.log(crontab.log_folder) + res.sendFile(crontab.log_folder +"/"+req.query.id+".log"); +}) + app.listen(app.get('port'), function() { console.log("Crontab UI is running at localhost:" + app.get('port')) }) diff --git a/crontab.js b/crontab.js index 9b7ad3f..5152696 100644 --- a/crontab.js +++ b/crontab.js @@ -7,7 +7,9 @@ var exec = require('child_process').exec; var fs = require('fs'); var cron_parser = require("cron-parser") -crontab = function(name, command, schedule, stopped){ +exports.log_folder = __dirname + '/crontabs/logs'; + +crontab = function(name, command, schedule, stopped, logging){ var data = {}; data.name = name; data.command = command; @@ -16,17 +18,18 @@ crontab = function(name, command, schedule, stopped){ data.stopped = stopped; } data.timestamp = (new Date()).toString(); + data.logging = logging; return data; } -exports.create_new = function(name, command, schedule){ - var tab = crontab(name, command, schedule, false); +exports.create_new = function(name, command, schedule, logging){ + var tab = crontab(name, command, schedule, false, logging); tab.created = new Date().valueOf(); db.insert(tab); } exports.update = function(data){ - db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null)); + db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null, data.logging)); } exports.status = function(_id, stopped){ @@ -52,7 +55,16 @@ exports.set_crontab = function(){ var crontab_string = ""; tabs.forEach(function(tab){ if(!tab.stopped){ - crontab_string += tab.schedule + " " + tab.command + "\n"; + if (tab.logging && tab.logging == "true"){ + tmp_log = "/tmp/" + tab._id + ".log"; + log_file = exports.log_folder + "/" + tab._id + ".log"; + if(tab.command[tab.command.length-1] != ";") // add semicolon + tab.command +=";" + //{ command; } 2>/tmp/.log|| {if test -f /tmp/; then date >> ; cat /tmp/.log >> ; rm /tmp.log } + crontab_string += tab.schedule + " { " + tab.command + " } 2> " + tmp_log +"; if test -f " + tmp_log +"; then date >> " + log_file + "; cat " + tmp_log + " >> " + log_file + "; rm " + tmp_log + "; fi \n"; + } + else + crontab_string += tab.schedule + " " + tab.command + "\n"; } }); fs.writeFile("/tmp/crontab", crontab_string, function(err) { diff --git a/crontabs/logs/README.md b/crontabs/logs/README.md new file mode 100644 index 0000000..9c37353 --- /dev/null +++ b/crontabs/logs/README.md @@ -0,0 +1 @@ +This folder will have all the log files diff --git a/public/js/script.js b/public/js/script.js index 77c231a..4e79575 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -74,13 +74,16 @@ function editJob(_id){ } schedule = job.schedule; job_command = job.command; + console.log(job.logging) + if (job.logging && job.logging != "false") + $("#job-logging").prop("checked", true); job_string(); } $("#job-save").unbind("click"); // remove existing events attached to this $("#job-save").click(function(){ // TODO good old boring validations - $.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id}, function(){ + $.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id, logging: $("#job-logging").prop("checked")}, function(){ location.reload(); }) }); @@ -102,7 +105,7 @@ function newJob(){ $("#job-save").unbind("click"); // remove existing events attached to this $("#job-save").click(function(){ // TODO good old boring validations - $.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1}, function(){ + $.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1, logging: $("#job-logging").prop("checked")}, function(){ location.reload(); }) }); diff --git a/routes.js b/routes.js index 7256561..15a629c 100644 --- a/routes.js +++ b/routes.js @@ -12,4 +12,5 @@ exports.routes = { "export": "/export", "import": "/import", // this is import from database "import_crontab": "/import_crontab", // this is from existing crontab + "logger": "/logger", } diff --git a/views/index.ejs b/views/index.ejs index 5f59a85..587275c 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -63,11 +63,14 @@ <%= crontab.command %> <%= crontab.schedule %> - <%= crontab.timestamp %> + <%= crontab.timestamp %> <% if (!crontab.stopped) { %> + <% if (crontab.logging && crontab.logging != "false") {%> + + <% } %> Edit Stop <% } else { %> diff --git a/views/popup.ejs b/views/popup.ejs index 77c5b1c..97f59df 100644 --- a/views/popup.ejs +++ b/views/popup.ejs @@ -71,6 +71,7 @@

+