-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathinstallCrontabs.php
More file actions
129 lines (107 loc) · 3.99 KB
/
Copy pathinstallCrontabs.php
File metadata and controls
129 lines (107 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Installs or updates RBT cron jobs in the system crontab.
*
* This function retrieves the current crontab, removes any existing RBT cron entries,
* and installs a fresh set of RBT cron jobs at various intervals (minutely, 5-minute,
* hourly, daily, weekly, and monthly). The existing crontab entries outside the RBT section
* are preserved.
*
* The function uses markers ("## RBT crons start, dont touch!!!" and
* "## RBT crons end, dont touch!!!") to identify and manage the RBT cron section.
*
* @global string $script_filename The path to the main script file to be executed by cron.
*
* @return int The number of cron job lines added (6 cron jobs + 2 marker lines = 8).
*
* @uses PHP_BINARY To get the PHP executable path.
* @uses exec() To retrieve the current crontab entries.
* @uses system() To install the updated crontab.
* @uses sys_get_temp_dir() To get the temporary directory for storing the crontab file.
*
* @throws None Errors are silently handled by system commands.
*/
function installCrontabs() {
global $script_filename;
$crontab = [];
exec("crontab -l", $crontab);
$clean = [];
$skip = false;
$cli = PHP_BINARY . " " . $script_filename . " --cron";
$lines = 0;
foreach ($crontab as $line) {
if ($line === "## RBT crons start, dont touch!!!") {
$skip = true;
}
if (!$skip) {
$clean[] = $line;
}
if ($line === "## RBT crons end, dont touch!!!") {
$skip = false;
}
}
$clean = explode("\n", trim(implode("\n", $clean)));
$clean[] = "";
$clean[] = "## RBT crons start, dont touch!!!";
$lines++;
$clean[] = "*/1 * * * * $cli=minutely";
$lines++;
$clean[] = "*/5 * * * * $cli=5min";
$lines++;
$clean[] = "1 */1 * * * $cli=hourly";
$lines++;
$clean[] = "1 1 */1 * * $cli=daily";
$lines++;
$clean[] = "1 1 * * 1 $cli=weekly";
$lines++;
$clean[] = "1 1 1 */1 * $cli=monthly";
$lines++;
$clean[] = "## RBT crons end, dont touch!!!";
$lines++;
file_put_contents(sys_get_temp_dir() . "/rbt_crontab", trim(implode("\n", $clean)) . "\n");
system("crontab " . sys_get_temp_dir() . "/rbt_crontab");
return $lines;
}
/**
* Removes RBT cron jobs from the system crontab.
*
* This function reads the current crontab, removes all lines between
* the RBT cron markers ("## RBT crons start, dont touch!!!" and
* "## RBT crons end, dont touch!!!"), and reinstalls the cleaned crontab.
*
* @return int The number of cron lines that were removed.
*
* @throws Exception If crontab operations fail during execution.
*
* @example
* $removedLines = unInstallCrontabs();
* echo "Removed $removedLines cron lines";
*/
function unInstallCrontabs() {
$crontab = [];
exec("crontab -l", $crontab);
$clean = [];
$skip = false;
$lines = 0;
foreach ($crontab as $line) {
if ($line === "## RBT crons start, dont touch!!!") {
$skip = true;
}
if (!$skip) {
$clean[] = $line;
} else {
$lines++;
}
if (strpos($line, "## RBT crons end, dont touch!!!") !== false) {
$skip = false;
$right = substr($line, strlen("## RBT crons end, dont touch!!!"));
if (trim($right)) {
$clean[] = $right;
}
}
}
$clean = explode("\n", trim(implode("\n", $clean)));
file_put_contents(sys_get_temp_dir() . "/rbt_crontab", trim(implode("\n", $clean)) . "\n");
system("crontab " . sys_get_temp_dir() . "/rbt_crontab");
return $lines;
}