crond.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * 禅道计划任务服务程序。
  4. * The crond for zentao.
  5. *
  6. * @copyright Copyright 2009-2013 QingDao Nature Easy Soft Network Technology Co,LTD (www.cnezsoft.com)
  7. * @license LGPL (http://www.gnu.org/licenses/lgpl.html)
  8. * @author jinyong zhu <zhujinyong@cnezsoft.com>
  9. * @package bin
  10. * @version $Id$
  11. * @link http://www.zentao.net
  12. */
  13. /* Set paths and timezone. */
  14. $zentaoPath = dirname(dirname(dirname(__FILE__))) . "/";
  15. $cronPath = $zentaoPath . 'bin/cron';
  16. include $zentaoPath . 'config/config.php';
  17. include $zentaoPath . 'lib/crontab/crontab.class.php';
  18. date_default_timezone_set($config->timezone);
  19. /* Parase crons. */
  20. $crons = parseCron($cronPath);
  21. $lastParsed = time();
  22. printCrons($crons);
  23. /* Start the cron demon. */
  24. while(true)
  25. {
  26. /* If need parse again, re parse the cron files. */
  27. if(needParseAgain($cronPath, $lastParsed))
  28. {
  29. echo "\ncron files changed, re parse them...";
  30. $crons = parseCron($cronPath);
  31. $lastParsed = time();
  32. printCrons($crons);
  33. }
  34. $now = new datetime('now');
  35. foreach($crons as $key => $cron)
  36. {
  37. if($now > $cron['time'])
  38. {
  39. $crons[$key]['time'] = $cron['cron']->getNextRunDate();
  40. $output = array();
  41. $log = '';
  42. exec($cron['command'], $output, $return);
  43. $time = $now->format('G:i:s');
  44. foreach($output as $out) $log .= $out . "\n";
  45. $log = "$time task " . ($key + 1) . " executed,\ncommand: $cron[command].\nreturn : $return.\noutput : $log\n";
  46. echo $log;
  47. logCron($log);
  48. }
  49. }
  50. sleep(40);
  51. }
  52. /* Parse cron file. */
  53. function parseCron($path)
  54. {
  55. chdir($path);
  56. $crons = array();
  57. $files = glob('*');
  58. foreach($files as $file)
  59. {
  60. $rows = file($file);
  61. foreach($rows as $row)
  62. {
  63. $row = preg_replace("/[ \t]+/", ' ', trim($row, " \t\n"));
  64. $row = preg_replace("/#.*/", '', $row);
  65. if($row)
  66. {
  67. preg_match_all('/(\S+\s+){5}|.*/', $row, $matches);
  68. if($matches[0])
  69. {
  70. $cron = array();
  71. $cron['schema'] = trim($matches[0][0]);
  72. $cron['command'] = trim($matches[0][1]);
  73. $cron['cron'] = CronExpression::factory($cron['schema']);
  74. $cron['time'] = $cron['cron']->getNextRunDate();
  75. $crons[] = $cron;
  76. }
  77. }
  78. }
  79. }
  80. return $crons;
  81. }
  82. /* Print crons. */
  83. function printCrons($crons)
  84. {
  85. echo "\n";
  86. echo 'total ' . count($crons) . " tasks found.\n\n";
  87. foreach($crons as $id => $cron)
  88. {
  89. echo ($id + 1) . "\t$cron[schema]\t$cron[command]\n";
  90. }
  91. }
  92. /* Log cron results. */
  93. function logCron($log)
  94. {
  95. $path = dirname(dirname(dirname(__FILE__))) . '/tmp/log/';
  96. $file = $path . 'cron.' . date('Ymd') . '.log';
  97. $fp = fopen($file, "a");
  98. fwrite($fp, $log);
  99. fclose ($fp);
  100. }
  101. /* Need parse cron files again? */
  102. function needParseAgain($cronPath, $lastParsed)
  103. {
  104. clearstatcache();
  105. chdir($cronPath);
  106. $files = glob('*');
  107. foreach($files as $file) if(filemtime($file) > $lastParsed) return true;
  108. return false;
  109. }