From 61a7b30d52b82d75c510685e15a9bf0ac4189a74 Mon Sep 17 00:00:00 2001 From: YavorAtanasov Date: Mon, 2 Dec 2013 20:49:55 +0200 Subject: [PATCH 1/2] added new method deleteJob. I added method for deleting jobs. --- crontab.php | 367 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 210 insertions(+), 157 deletions(-) diff --git a/crontab.php b/crontab.php index 5973602..b425686 100644 --- a/crontab.php +++ b/crontab.php @@ -27,170 +27,223 @@ */ 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); + + /** + * 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 = '*'; + + /** + * Year 1970–2099 + * @var string + */ + var $year = null; + /** + * @var array + */ + var $jobs = array(); - 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; + /** + * @var array + */ + var $old_jobs = array(); + + function Crontab() { + } - return $this; + function __construct(){ + + $this -> old_jobs = $this->listJobs(); } - - /** - * 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"; + + /** + * 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 Year + * @param string $year required + * @return object + */ + function onYear($year) { + $this->year = $year; + 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, + $this->year + ) = 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) { + $year = !is_null($this->year) ? ' '.$this->year : ''; + $this->jobs[] = $this->minute.' '. + $this->hour.' '. + $this->dayOfMonth.' '. + $this->month.' '. + $this->dayOfWeek. + $year."\t". + $job; + + return $this; + } + + /** + * Save the jobs to disk, remove existing cron + * @param boolean $includeOldJobs optional + * @return boolean + */ + function activate($includeOldJobs = true) { + $contents = ''; if($includeOldJobs) { - $contents .= $this->listJobs(); - } - - if(is_writable($this->destination) || !file_exists($this->destination)){ - exec($this->crontab.' -r;'); + $contents .= implode("\n", $this->old_jobs); + $contents .= "\n"; + } + $contents .= implode("\n", $this->jobs); + $contents .= "\n"; + + if(is_writable($this->destination) || !file_exists($this->destination)){ file_put_contents($this->destination, $contents, LOCK_EX); - exec($this->crontab.' '.$this->destination.';'); - return true; - } + exec($this->crontab.' -r;'); + exec($this->crontab.' '.$this->destination.';'); + return true; + } + + return false; + } + + /** + * List current cron jobs + * @return array + */ + function listJobs() { + exec($this->crontab.' -l;', $tmp); + return $tmp; + } - return false; - } - - /** - * List current cron jobs - * @return string - */ - function listJobs() { - return exec($this->crontab.' -l;'); - } + /** + * Delete one job from current jobs. + * @return array + */ + function deleteJob($job = null) { + if (!is_null($job)){ + $data = array(); + $tmp = $this -> old_jobs; + $rowsDeleted = 0; + if (is_array($tmp)){ + foreach($tmp as $val){ + if(!preg_match('/'.$job.'/',$val)){ + $data[] = $val; + } else { + $rowsDeleted++; + } + } + } + $this -> old_jobs = $data; + } + return $rowsDeleted; + } } ?> From e592f6df9bb7663b20da80d5d7b16a6175e403a7 Mon Sep 17 00:00:00 2001 From: YavorAtanasov Date: Tue, 3 Dec 2013 19:37:29 +0200 Subject: [PATCH 2/2] Update crontab.php --- crontab.php | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/crontab.php b/crontab.php index b425686..4e3e979 100644 --- a/crontab.php +++ b/crontab.php @@ -70,11 +70,7 @@ class Crontab { */ var $dayOfWeek = '*'; - /** - * Year 1970–2099 - * @var string - */ - var $year = null; + /** * @var array */ @@ -143,15 +139,6 @@ function onDayOfWeek($dayOfWeek) { return $this; } - /** - * Set Year - * @param string $year required - * @return object - */ - function onYear($year) { - $this->year = $year; - 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 @@ -164,8 +151,7 @@ function on($timeCode) { $this->hour, $this->dayOfMonth, $this->month, - $this->dayOfWeek, - $this->year + $this->dayOfWeek ) = explode(' ', $timeCode); return $this; @@ -177,13 +163,11 @@ function on($timeCode) { * @return object */ function doJob($job) { - $year = !is_null($this->year) ? ' '.$this->year : ''; $this->jobs[] = $this->minute.' '. $this->hour.' '. $this->dayOfMonth.' '. $this->month.' '. - $this->dayOfWeek. - $year."\t". + $this->dayOfWeek."\t". $job; return $this;