You've already forked crontab-ui
mirror of
https://github.com/alseambusher/crontab-ui.git
synced 2025-12-15 11:43:58 +08:00
1. version bump
2. Update README.md
This commit is contained in:
56
crontab.js
56
crontab.js
@@ -5,8 +5,8 @@ db.loadDatabase(function (err) {
|
||||
});
|
||||
var exec = require('child_process').exec;
|
||||
var fs = require('fs');
|
||||
var cron_parser = require("cron-parser")
|
||||
var os = require("os")
|
||||
var cron_parser = require("cron-parser");
|
||||
var os = require("os");
|
||||
|
||||
exports.log_folder = __dirname + '/crontabs/logs';
|
||||
exports.env_file = __dirname + '/crontabs/env.db';
|
||||
@@ -22,36 +22,36 @@ crontab = function(name, command, schedule, stopped, logging){
|
||||
data.timestamp = (new Date()).toString();
|
||||
data.logging = logging;
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
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, data.logging));
|
||||
}
|
||||
};
|
||||
|
||||
exports.status = function(_id, stopped){
|
||||
db.update({_id: _id},{$set: {stopped: stopped}});
|
||||
}
|
||||
};
|
||||
|
||||
exports.remove = function(_id){
|
||||
db.remove({_id: _id}, {});
|
||||
}
|
||||
};
|
||||
exports.crontabs = function(callback){
|
||||
db.find({}).sort({ created: -1 }).exec(function(err, docs){
|
||||
for(var i=0; i<docs.length; i++){
|
||||
if(docs[i].schedule == "@reboot")
|
||||
docs[i].next = "Next Reboot"
|
||||
docs[i].next = "Next Reboot";
|
||||
else
|
||||
docs[i].next = cron_parser.parseExpression(docs[i].schedule).next().toString();
|
||||
}
|
||||
callback(docs);
|
||||
});
|
||||
}
|
||||
};
|
||||
exports.set_crontab = function(env_vars){
|
||||
exports.crontabs( function(tabs){
|
||||
var crontab_string = "";
|
||||
@@ -64,7 +64,7 @@ exports.set_crontab = function(env_vars){
|
||||
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 +=";"
|
||||
tab.command +=";";
|
||||
//{ command; } 2>/tmp/<id>.log|| {if test -f /tmp/<id>; then date >> <log file>; cat /tmp/<id>.log >> <log file>; rm /tmp<id>.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";
|
||||
}
|
||||
@@ -79,10 +79,10 @@ exports.set_crontab = function(env_vars){
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
exports.get_backup_names = function(){
|
||||
var backups = []
|
||||
var backups = [];
|
||||
fs.readdirSync(__dirname + '/crontabs').forEach(function(file){
|
||||
// file name begins with backup
|
||||
if(file.indexOf("backup") == 0){
|
||||
@@ -92,10 +92,10 @@ exports.get_backup_names = function(){
|
||||
|
||||
// Sort by date. Newest on top
|
||||
for(var i=0; i<backups.length; i++){
|
||||
var Ti = backups[i].split("backup")[1]
|
||||
var Ti = backups[i].split("backup")[1];
|
||||
Ti = new Date(Ti.substring(0, Ti.length-3)).valueOf();
|
||||
for(var j=0; j<i; j++){
|
||||
var Tj = backups[j].split("backup")[1]
|
||||
var Tj = backups[j].split("backup")[1];
|
||||
Tj = new Date(Tj.substring(0, Tj.length-3)).valueOf();
|
||||
if(Ti > Tj){
|
||||
var temp = backups[i];
|
||||
@@ -106,50 +106,48 @@ exports.get_backup_names = function(){
|
||||
}
|
||||
|
||||
return backups;
|
||||
}
|
||||
};
|
||||
|
||||
exports.backup = function(){
|
||||
//TODO check if it failed
|
||||
fs.createReadStream( __dirname + '/crontabs/crontab.db').pipe(fs.createWriteStream( __dirname + '/crontabs/backup ' + (new Date()).toString().replace("+", " ") + '.db'));
|
||||
}
|
||||
};
|
||||
|
||||
exports.restore = function(db_name){
|
||||
fs.createReadStream( __dirname + '/crontabs/' + db_name).pipe(fs.createWriteStream( __dirname + '/crontabs/crontab.db'));
|
||||
db.loadDatabase(); // reload the database
|
||||
}
|
||||
};
|
||||
|
||||
exports.reload_db= function(){
|
||||
db.loadDatabase();
|
||||
}
|
||||
};
|
||||
|
||||
exports.get_env = function(){
|
||||
if (fs.existsSync(exports.env_file)) {
|
||||
return fs.readFileSync(exports.env_file , 'utf8').replace("\n", "\n");
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return "";
|
||||
};
|
||||
|
||||
// TODO
|
||||
exports.import_crontab = function(){
|
||||
exec("crontab -l", function(error, stdout, stderr){
|
||||
var lines = stdout.split("\n");
|
||||
|
||||
var namePrefix = new Date().getTime();
|
||||
|
||||
lines.forEach(function(line, index){
|
||||
var regex = /^((\@[a-zA-Z]+\s)|(([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s))/;
|
||||
var command = line.replace(regex, '').trim();
|
||||
var schedule = line.replace(command, '').trim();
|
||||
|
||||
|
||||
if(command && schedule){
|
||||
var name = namePrefix + '_' + index;
|
||||
|
||||
db.crontabs.findOne({ command: command, schedule: schedule }, function(err, doc) {
|
||||
db.findOne({ command: command, schedule: schedule }, function(err, doc) {
|
||||
if(err) {
|
||||
throw err;
|
||||
}
|
||||
if(!doc){
|
||||
exports.create_new(name, command, null, null, schedule, null);
|
||||
exports.create_new(name, command, schedule, null);
|
||||
}
|
||||
else{
|
||||
doc.command = command;
|
||||
@@ -157,11 +155,7 @@ exports.import_crontab = function(){
|
||||
exports.update(doc);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
//console.log(stdout);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user