From d875876e33f7a019af444571995e0ff83f751545 Mon Sep 17 00:00:00 2001 From: wangchunsheng Date: Wed, 30 Jan 2013 05:28:51 +0000 Subject: [PATCH] * when parsed crons, print them. + add feature of re parse. * adjust the log format and save path. --- bin/php/crond.php | 69 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/bin/php/crond.php b/bin/php/crond.php index ab4155a359..36f1904dee 100755 --- a/bin/php/crond.php +++ b/bin/php/crond.php @@ -1,13 +1,38 @@ + * @package bin + * @version $Id$ + * @link http://www.zentao.net + */ +/* Set pathes and include crontab.class.php. */ +$zentaoPath = dirname(dirname(dirname(__FILE__))) . "\\"; +$cronPath = dirname(dirname(dirname(__FILE__))) . '/bin/cron'; +include $zentaoPath . '/lib/crontab/crontab.class.php'; +/* Parase crons. */ $crons = parseCron($cronPath); +$lastParsed = time(); +printCrons($crons); -/* run as daemon. */ -while(1) +/* Start the cron demon. */ +while(true) { - $now = new DateTime('now'); + /* If need parse again, re parse the cron files. */ + if(needParseAgain($cronPath, $lastParsed)) + { + echo "\ncron files changed, re parse them..."; + $crons = parseCron($cronPath); + $lastParsed = time(); + printCrons($crons); + } + + $now = new datetime('now'); foreach($crons as $key => $cron) { if($now > $cron['time']) @@ -18,9 +43,9 @@ while(1) $log = ''; exec($cron['command'], $output, $return); - $time = $now->format('Y-m-d H:i:s'); + $time = $now->format('H:i:s'); foreach($output as $out) $log .= $out . "\n"; - $log = $time . ' ' . $key . ' return ' . $return . ' : ' . $log . "\n"; + $log = "$time task " . ($key + 1) . " executed,\ncommand: $cron[command].\nreturn : $return.\noutput : $log\n"; echo $log; logCron($log); } @@ -31,10 +56,8 @@ while(1) /* Log cron results. */ function logCron($log) { - $path = dirname(dirname(dirname(__FILE__))) . '/tmp/cron'; - $file = $path . '/' . date('Ymd'); - - if(!file_exists($path)) mkdir($path, 0777); + $path = dirname(dirname(dirname(__FILE__))) . '/tmp/log/'; + $file = $path . 'cron.' . date('Ymd') . '.log'; $fp = fopen($file, "a"); fwrite($fp, $log); @@ -44,8 +67,9 @@ function logCron($log) /* Parse cron file. */ function parseCron($path) { - $crons = array(); chdir($path); + + $crons = array(); $files = glob('*'); foreach($files as $file) { @@ -71,3 +95,24 @@ function parseCron($path) } return $crons; } + +function printCrons($crons) +{ + echo "\n"; + echo 'total ' . count($crons) . " tasks found.\n\n"; + foreach($crons as $id => $cron) + { + echo ($id + 1) . "\t$cron[schema]\t$cron[command]\n"; + } +} + +/* Need parse cron files again? */ +function needParseAgain($cronPath, $lastParsed) +{ + clearstatcache(); + chdir($cronPath); + + $files = glob('*'); + foreach($files as $file) if(filemtime($file) > $lastParsed) return true; + return false; +}