-
Notifications
You must be signed in to change notification settings - Fork 87
/
crontab.php
196 lines (171 loc) · 4.2 KB
/
crontab.php
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?
/**
* @author Ryan Faerman <[email protected]>
* @version 0.1
* @package PHPCronTab
*
* Copyright (c) 2009 Ryan Faerman <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
class Crontab {
/**
* Location of the crontab executable
* @var string
*/
var $crontab = '/usr/bin/crontab';
/**
* Location to save the crontab file.
* @var string
*/
var $destination = '/tmp/CronManager';
/**
* Minute (0 - 59)
* @var string
*/
var $minute = 0;
/**
* Hour (0 - 23)
* @var string
*/
var $hour = 10;
/**
* Day of Month (1 - 31)
* @var string
*/
var $dayOfMonth = '*';
/**
* Month (1 - 12) OR jan,feb,mar,apr...
* @var string
*/
var $month = '*';
/**
* Day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
* @var string
*/
var $dayOfWeek = '*';
/**
* @var array
*/
var $jobs = array();
function Crontab() {
}
/**
* Set minute or minutes
* @param string $minute required
* @return object
*/
function onMinute($minute) {
$this->minute = $minute;
return $this;
}
/**
* Set hour or hours
* @param string $hour required
* @return object
*/
function onHour($hour) {
$this->hour = $hour;
return $this;
}
/**
* Set day of month or days of month
* @param string $dayOfMonth required
* @return object
*/
function onDayOfMonth($dayOfMonth) {
$this->dayOfMonth = $dayOfMonth;
return $this;
}
/**
* Set month or months
* @param string $month required
* @return object
*/
function onMonth($month) {
$this->month = $month;
return $this;
}
/**
* Set day of week or days of week
* @param string $minute required
* @return object
*/
function onDayOfWeek($dayOfWeek) {
$this->dayOfWeek = $dayOfWeek;
return $this;
}
/**
* Set entire time code with one function. This has to be a complete entry. See http://en.wikipedia.org/wiki/Cron#crontab_syntax
* @param string $timeCode required
* @return object
*/
function on($timeCode) {
list(
$this->minute,
$this->hour,
$this->dayOfMonth,
$this->month,
$this->dayOfWeek
) = explode(' ', $timeCode);
return $this;
}
/**
* Add job to the jobs array. Each time segment should be set before calling this method. The job should include the absolute path to the commands being used.
* @param string $job required
* @return object
*/
function doJob($job) {
$this->jobs[] = $this->minute.' '.
$this->hour.' '.
$this->dayOfMonth.' '.
$this->month.' '.
$this->dayOfWeek.' '.
$job;
return $this;
}
/**
* Save the jobs to disk, remove existing cron
* @param boolean $includeOldJobs optional
* @return boolean
*/
function activate($includeOldJobs = true) {
$contents = implode("\n", $this->jobs);
$contents .= "\n";
if($includeOldJobs) {
$contents .= $this->listJobs();
}
if(is_writable($this->destination) || !file_exists($this->destination)){
exec($this->crontab.' -r;');
file_put_contents($this->destination, $contents, LOCK_EX);
exec($this->crontab.' '.$this->destination.';');
return true;
}
return false;
}
/**
* List current cron jobs
* @return string
*/
function listJobs() {
return exec($this->crontab.' -l;');
}
}
?>