201
by Tony George
Updated project structure |
1 |
using TeeJee.Logging; |
2 |
using TeeJee.FileSystem; |
|
3 |
//using TeeJee.JSON;
|
|
4 |
using TeeJee.ProcessHelper; |
|
5 |
//using TeeJee.Multimedia;
|
|
6 |
//using TeeJee.System;
|
|
7 |
using TeeJee.Misc; |
|
8 |
||
9 |
public class CronTab : GLib.Object { |
|
10 |
||
11 |
public static string crontab_read_all(string user_name = ""){ |
|
12 |
string std_out, std_err; |
|
13 |
||
14 |
var cmd = "crontab -l"; |
|
15 |
if (user_name.length > 0){ |
|
16 |
cmd += " -u %s".printf(user_name); |
|
17 |
}
|
|
18 |
||
19 |
log_debug(cmd); |
|
20 |
||
21 |
int ret_val = exec_sync(cmd, out std_out, out std_err); |
|
22 |
||
23 |
if (ret_val != 0){ |
|
24 |
log_debug(_("Failed to read cron tab")); |
|
25 |
return ""; |
|
26 |
}
|
|
27 |
else{ |
|
28 |
return std_out; |
|
29 |
}
|
|
30 |
}
|
|
31 |
||
32 |
public static bool add_job(string entry){ |
|
33 |
// read crontab file
|
|
34 |
string tab = crontab_read_all(); |
|
35 |
var lines = new Gee.ArrayList<string>(); |
|
36 |
foreach(string line in tab.split("\n")){ |
|
37 |
lines.add(line); |
|
38 |
}
|
|
39 |
||
40 |
// check if entry exists
|
|
41 |
foreach(string line in lines){ |
|
42 |
if (line == entry){ |
|
43 |
return true; // return |
|
44 |
}
|
|
45 |
}
|
|
46 |
||
47 |
// append entry
|
|
48 |
lines.add(entry); |
|
49 |
||
50 |
// create new tab
|
|
51 |
string tab_new = ""; |
|
52 |
foreach(string line in lines){ |
|
53 |
if (line.length > 0){ |
|
54 |
tab_new += "%s\n".printf(line); |
|
55 |
}
|
|
56 |
}
|
|
57 |
||
58 |
// write temp crontab file
|
|
59 |
string temp_file = get_temp_file_path(); |
|
60 |
file_write(temp_file, tab_new); |
|
61 |
||
62 |
// install crontab file
|
|
63 |
var cmd = "crontab \"%s\"".printf(temp_file); |
|
64 |
int status = exec_sync(cmd); |
|
65 |
||
66 |
if (status != 0){ |
|
67 |
log_error(_("Failed to add cron job") + ": %s".printf(entry)); |
|
68 |
return false; |
|
69 |
}
|
|
70 |
else{ |
|
71 |
log_msg(_("Cron job added") + ": %s".printf(entry)); |
|
72 |
return true; |
|
73 |
}
|
|
74 |
}
|
|
75 |
||
76 |
public static bool remove_job(string entry){ |
|
77 |
// read crontab file
|
|
78 |
string tab = crontab_read_all(); |
|
79 |
var lines = new Gee.ArrayList<string>(); |
|
80 |
foreach(string line in tab.split("\n")){ |
|
81 |
lines.add(line); |
|
82 |
}
|
|
83 |
||
84 |
// check if entry exists
|
|
85 |
bool found = false; |
|
86 |
for(int i=0; i < lines.size; i++){ |
|
87 |
string line = lines[i]; |
|
88 |
if (line != null){ |
|
89 |
line = line.strip(); |
|
90 |
}
|
|
91 |
if (line == entry){ |
|
92 |
lines.remove(line); |
|
93 |
found = true; |
|
94 |
}
|
|
95 |
}
|
|
96 |
if (!found){ |
|
97 |
return true; |
|
98 |
}
|
|
99 |
||
100 |
// create new tab
|
|
101 |
string tab_new = ""; |
|
102 |
foreach(string line in lines){ |
|
103 |
if (line.length > 0){ |
|
104 |
tab_new += "%s\n".printf(line); |
|
105 |
}
|
|
106 |
}
|
|
107 |
||
108 |
// write temp crontab file
|
|
109 |
string temp_file = get_temp_file_path(); |
|
110 |
file_write(temp_file, tab_new); |
|
111 |
||
112 |
// install crontab file
|
|
113 |
var cmd = "crontab \"%s\"".printf(temp_file); |
|
114 |
int status = exec_sync(cmd); |
|
115 |
||
116 |
if (status != 0){ |
|
117 |
log_error(_("Failed to remove cron job") + ": %s".printf(entry)); |
|
118 |
return false; |
|
119 |
}
|
|
120 |
else{ |
|
121 |
log_msg(_("Cron job removed") + ": %s".printf(entry)); |
|
122 |
return true; |
|
123 |
}
|
|
124 |
}
|
|
125 |
||
126 |
public static bool install(string file_path, string user_name = ""){ |
|
127 |
||
128 |
if (!file_exists(file_path)){ |
|
129 |
log_error(_("File not found") + ": %s".printf(file_path)); |
|
130 |
return false; |
|
131 |
}
|
|
132 |
||
133 |
var cmd = "crontab"; |
|
134 |
if (user_name.length > 0){ |
|
135 |
cmd += " -u %s".printf(user_name); |
|
136 |
}
|
|
137 |
cmd += " \"%s\"".printf(file_path); |
|
138 |
||
139 |
log_debug(cmd); |
|
140 |
||
141 |
int status = exec_sync(cmd); |
|
142 |
||
143 |
if (status != 0){ |
|
144 |
log_error(_("Failed to install crontab file") + ": %s".printf(file_path)); |
|
145 |
return false; |
|
146 |
}
|
|
147 |
else{ |
|
148 |
log_msg(_("crontab file installed") + ": %s".printf(file_path)); |
|
149 |
return true; |
|
150 |
}
|
|
151 |
}
|
|
152 |
||
153 |
public static bool export(string file_path, string user_name = ""){ |
|
154 |
if (file_exists(file_path)){ |
|
155 |
file_delete(file_path); |
|
156 |
}
|
|
157 |
||
158 |
bool ok = file_write(file_path, crontab_read_all(user_name)); |
|
159 |
||
160 |
if (!ok){ |
|
161 |
log_error(_("Failed to export crontab file") + ": %s".printf(file_path)); |
|
162 |
return false; |
|
163 |
}
|
|
164 |
else{ |
|
165 |
log_msg(_("crontab file exported") + ": %s".printf(file_path)); |
|
166 |
return true; |
|
167 |
}
|
|
168 |
}
|
|
169 |
||
170 |
public static bool import(string file_path, string user_name = ""){ |
|
171 |
return install(file_path, user_name); |
|
172 |
}
|
|
173 |
}
|
|
174 |