3
using TeeJee.FileSystem;
5
using TeeJee.ProcessHelper;
6
//using TeeJee.Multimedia;
10
public class CronTab : GLib.Object {
12
public static string crontab_read_all(string user_name = ""){
13
string std_out, std_err;
15
var cmd = "crontab -l";
16
if (user_name.length > 0){
17
cmd += " -u %s".printf(user_name);
22
int ret_val = exec_sync(cmd, out std_out, out std_err);
25
log_debug(_("Failed to read cron tab"));
33
public static bool add_job(string entry){
35
string tab = crontab_read_all();
36
var lines = new Gee.ArrayList<string>();
37
foreach(string line in tab.split("\n")){
41
// check if entry exists
42
foreach(string line in lines){
44
return true; // return
53
foreach(string line in lines){
55
tab_new += "%s\n".printf(line);
59
// write temp crontab file
60
string temp_file = get_temp_file_path();
61
file_write(temp_file, tab_new);
63
// install crontab file
64
var cmd = "crontab \"%s\"".printf(temp_file);
65
int status = exec_sync(cmd);
68
log_error(_("Failed to add cron job") + ": %s".printf(entry));
72
log_msg(_("Cron job added") + ": %s".printf(entry));
77
public static bool remove_job(string entry){
79
string tab = crontab_read_all();
80
var lines = new Gee.ArrayList<string>();
81
foreach(string line in tab.split("\n")){
85
// check if entry exists
87
for(int i=0; i < lines.size; i++){
88
string line = lines[i];
103
foreach(string line in lines){
104
if (line.length > 0){
105
tab_new += "%s\n".printf(line);
109
// write temp crontab file
110
string temp_file = get_temp_file_path();
111
file_write(temp_file, tab_new);
113
// install crontab file
114
var cmd = "crontab \"%s\"".printf(temp_file);
115
int status = exec_sync(cmd);
118
log_error(_("Failed to remove cron job") + ": %s".printf(entry));
122
log_msg(_("Cron job removed") + ": %s".printf(entry));
127
public static bool install(string file_path, string user_name = ""){
129
if (!file_exists(file_path)){
130
log_error(_("File not found") + ": %s".printf(file_path));
135
if (user_name.length > 0){
136
cmd += " -u %s".printf(user_name);
138
cmd += " \"%s\"".printf(file_path);
142
int status = exec_sync(cmd);
145
log_error(_("Failed to install crontab file") + ": %s".printf(file_path));
149
log_msg(_("crontab file installed") + ": %s".printf(file_path));
154
public static bool export(string file_path, string user_name = ""){
155
if (file_exists(file_path)){
156
file_delete(file_path);
159
bool ok = file_write(file_path, crontab_read_all(user_name));
162
log_error(_("Failed to export crontab file") + ": %s".printf(file_path));
166
log_msg(_("crontab file exported") + ": %s".printf(file_path));
171
public static bool import(string file_path, string user_name = ""){
172
return install(file_path, user_name);