From 88c08b455cbc9bd9f6da707b79acf4c355570b65 Mon Sep 17 00:00:00 2001 From: zhujinyong Date: Mon, 14 Jan 2013 03:15:50 +0000 Subject: [PATCH] * code for cron. --- bin/crontab/config.php | 4 --- bin/crontab/crontab.php | 48 +++++++++++++++++++++++++++++----- bin/crontab/tasks/default.cron | 2 ++ 3 files changed, 43 insertions(+), 11 deletions(-) delete mode 100644 bin/crontab/config.php create mode 100644 bin/crontab/tasks/default.cron diff --git a/bin/crontab/config.php b/bin/crontab/config.php deleted file mode 100644 index 089c824ed5..0000000000 --- a/bin/crontab/config.php +++ /dev/null @@ -1,4 +0,0 @@ -backup = array('schema' => '1 1 * * *', 'script' => '../php/backup.php'); -$crontab->computeburn = array('schema' => '1 23 * * *', 'script' => '../php/computeburn.php'); diff --git a/bin/crontab/crontab.php b/bin/crontab/crontab.php index 0233350fb4..7186b79772 100755 --- a/bin/crontab/crontab.php +++ b/bin/crontab/crontab.php @@ -1,10 +1,8 @@ $cron) +foreach($crons as $key => $cron) { $tasks[$key] = new stdClass(); $tasks[$key]->cron = CronExpression::factory($cron['schema']); @@ -14,12 +12,12 @@ foreach($crontab as $key => $cron) /* run as daemon. */ while(1) { - foreach($crontab as $key => $cron) + foreach($crons as $key => $cron) { if($tasks[$key]->cron->getNextRunDate()->format('Y-m-d H:i') != $tasks[$key]->time) { $time = date('Y-m-d H:i:s'); - $output = system('php ' . $cron['script'], $retval); + $output = system($cron['command'], $retval); $content = $time . ' ' . $key . ' return ' . $retval . ' : ' . $output . "\n"; logCron($content); $tasks[$key]->time = $tasks[$key]->cron->getNextRunDate()->format('Y-m-d H:i'); @@ -28,7 +26,7 @@ while(1) sleep(60); } -/* log cron results. */ +/* Log cron results. */ function logCron($content) { $file = dirname(dirname(dirname(__FILE__))) . '/tmp/cron.log'; @@ -36,3 +34,39 @@ function logCron($content) fwrite($fp, $content); fclose ($fp); } + +/* Parse cron file. */ +function parseCron($path = 'config') +{ + $crons = array(); + chdir($path); + $files = glob('*'); + foreach($files as $file) + { + $handle = fopen($file, 'r'); + $content = fread($handle, filesize($file)); + fclose($handle); + + $rows = explode("\n", $content); + if($rows) + { + foreach($rows as $row) + { + $mod = "/#.*/"; + $row = preg_replace($mod, '', $row); + if($row) + { + preg_match_all('/(\S+\s+){5}|.*/', $row, $matchs); + if($matchs[0]) + { + $cron = array(); + $cron['schema'] = trim($matchs[0][0], "\t "); + $cron['command'] = trim($matchs[0][1], "\t "); + $crons[] = $cron; + } + } + } + } + } + return $crons; +} diff --git a/bin/crontab/tasks/default.cron b/bin/crontab/tasks/default.cron new file mode 100644 index 0000000000..e7a167e114 --- /dev/null +++ b/bin/crontab/tasks/default.cron @@ -0,0 +1,2 @@ +#crontab +* * * * * php ../../php/backup.php # aaaa