└── crontab.php /crontab.php: -------------------------------------------------------------------------------- 1 | 4 | * @version 0.1 5 | * @package PHPCronTab 6 | * 7 | * Copyright (c) 2009 Ryan Faerman 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | * 27 | */ 28 | 29 | class Crontab { 30 | 31 | /** 32 | * Location of the crontab executable 33 | * @var string 34 | */ 35 | var $crontab = '/usr/bin/crontab'; 36 | 37 | /** 38 | * Location to save the crontab file. 39 | * @var string 40 | */ 41 | var $destination = '/tmp/CronManager'; 42 | 43 | /** 44 | * Minute (0 - 59) 45 | * @var string 46 | */ 47 | var $minute = 0; 48 | 49 | /** 50 | * Hour (0 - 23) 51 | * @var string 52 | */ 53 | var $hour = 10; 54 | 55 | /** 56 | * Day of Month (1 - 31) 57 | * @var string 58 | */ 59 | var $dayOfMonth = '*'; 60 | 61 | /** 62 | * Month (1 - 12) OR jan,feb,mar,apr... 63 | * @var string 64 | */ 65 | var $month = '*'; 66 | 67 | /** 68 | * Day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 69 | * @var string 70 | */ 71 | var $dayOfWeek = '*'; 72 | 73 | /** 74 | * @var array 75 | */ 76 | var $jobs = array(); 77 | 78 | function Crontab() { 79 | } 80 | 81 | /** 82 | * Set minute or minutes 83 | * @param string $minute required 84 | * @return object 85 | */ 86 | function onMinute($minute) { 87 | $this->minute = $minute; 88 | return $this; 89 | } 90 | 91 | /** 92 | * Set hour or hours 93 | * @param string $hour required 94 | * @return object 95 | */ 96 | function onHour($hour) { 97 | $this->hour = $hour; 98 | return $this; 99 | } 100 | 101 | /** 102 | * Set day of month or days of month 103 | * @param string $dayOfMonth required 104 | * @return object 105 | */ 106 | function onDayOfMonth($dayOfMonth) { 107 | $this->dayOfMonth = $dayOfMonth; 108 | return $this; 109 | } 110 | 111 | /** 112 | * Set month or months 113 | * @param string $month required 114 | * @return object 115 | */ 116 | function onMonth($month) { 117 | $this->month = $month; 118 | return $this; 119 | } 120 | 121 | /** 122 | * Set day of week or days of week 123 | * @param string $minute required 124 | * @return object 125 | */ 126 | function onDayOfWeek($dayOfWeek) { 127 | $this->dayOfWeek = $dayOfWeek; 128 | return $this; 129 | } 130 | 131 | /** 132 | * Set entire time code with one function. This has to be a complete entry. See http://en.wikipedia.org/wiki/Cron#crontab_syntax 133 | * @param string $timeCode required 134 | * @return object 135 | */ 136 | function on($timeCode) { 137 | list( 138 | $this->minute, 139 | $this->hour, 140 | $this->dayOfMonth, 141 | $this->month, 142 | $this->dayOfWeek 143 | ) = explode(' ', $timeCode); 144 | 145 | return $this; 146 | } 147 | 148 | /** 149 | * 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. 150 | * @param string $job required 151 | * @return object 152 | */ 153 | function doJob($job) { 154 | $this->jobs[] = $this->minute.' '. 155 | $this->hour.' '. 156 | $this->dayOfMonth.' '. 157 | $this->month.' '. 158 | $this->dayOfWeek.' '. 159 | $job; 160 | 161 | return $this; 162 | } 163 | 164 | /** 165 | * Save the jobs to disk, remove existing cron 166 | * @param boolean $includeOldJobs optional 167 | * @return boolean 168 | */ 169 | function activate($includeOldJobs = true) { 170 | $contents = implode("\n", $this->jobs); 171 | $contents .= "\n"; 172 | 173 | if($includeOldJobs) { 174 | $contents .= $this->listJobs(); 175 | } 176 | 177 | if(is_writable($this->destination) || !file_exists($this->destination)){ 178 | exec($this->crontab.' -r;'); 179 | file_put_contents($this->destination, $contents, LOCK_EX); 180 | exec($this->crontab.' '.$this->destination.';'); 181 | return true; 182 | } 183 | 184 | return false; 185 | } 186 | 187 | /** 188 | * List current cron jobs 189 | * @return string 190 | */ 191 | function listJobs() { 192 | return exec($this->crontab.' -l;'); 193 | } 194 | } 195 | 196 | ?> 197 | --------------------------------------------------------------------------------