当前位置:首页 > PHP教程 > php应用 > 列表

PHP中实现crontab代码分享

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-18 20:26:53 浏览: 评论:0 

这篇文章主要介绍了PHP中实现crontab代码分享,本文给出了实现代码和使用方法,需要的朋友可以参考下。

1. 准备一个标准crontab文件 ./crontab:

# m h dom mon dow command

* * * * * date > /tmp/cron.date.run

2. crontab -e 将此cron.php脚本加入系统cron,代码如下:

* * * * * /usr/bin/php cron.php

3. cron.php 源码:

  1. // 从./crontab读取cron项,也可以从其他持久存储(mysql、redis)读取 
  2. $crontab = file('./crontab'); 
  3. $now = $_SERVER['REQUEST_TIME']; 
  4. foreach ( $crontab as $cron ) { 
  5.  $slices = preg_split("/[\s]+/", $cron, 6); 
  6.  if( count($slices) !== 6 ) continue; 
  7.  
  8.  $cmd       = array_pop($slices); 
  9.  $cron_time = implode(' ', $slices); 
  10.  $next_time = Crontab::parse($cron_time, $now); 
  11.  if ( $next_time !== $now ) continue;  
  12.  
  13.  $pid = pcntl_fork(); 
  14.  if ($pid == -1) { 
  15.   die('could not fork'); 
  16.  } else if ($pid) { 
  17.   // we are the parent 
  18.   pcntl_wait($status, WNOHANG); //Protect against Zombie children 
  19.  } else { 
  20.       // we are the child 
  21.   `$cmd`; 
  22.   exit; 
  23.  } 
  24. } 
  25.  
  26. /* https://github.com/jkonieczny/PHP-Crontab */ 
  27. class Crontab { 
  28.    /** 
  29.  * Finds next execution time(stamp) parsin crontab syntax, 
  30.  * after given starting timestamp (or current time if ommited) 
  31.  * 
  32.  * @param string $_cron_string: 
  33.  * 
  34.  * 0 1 2 3 4 
  35.  * * * * * * 
  36.  * - - - - - 
  37.  * | | | | | 
  38.  * | | | | +----- day of week (0 - 6) (Sunday=0) 
  39.  * | | | +------- month (1 - 12) 
  40.  * | | +--------- day of month (1 - 31) 
  41.  * | +----------- hour (0 - 23) 
  42.  * +------------- min (0 - 59) 
  43.  * @param int $_after_timestamp timestamp [default=current timestamp] 
  44.  * @return int unix timestamp - next execution time will be greater 
  45.  * than given timestamp (defaults to the current timestamp) 
  46.  * @throws InvalidArgumentException 
  47.  */ 
  48.     public static function parse($_cron_string,$_after_timestamp=null) 
  49.     { 
  50.         if(!preg_match('/^((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)$/i',trim($_cron_string))){ 
  51.             throw new InvalidArgumentException("Invalid cron string: ".$_cron_string); 
  52.         } 
  53.         if($_after_timestamp && !is_numeric($_after_timestamp)){ 
  54.             throw new InvalidArgumentException("\$_after_timestamp must be a valid unix timestamp ($_after_timestamp given)"); 
  55.         } 
  56.         $cron = preg_split("/[\s]+/i",trim($_cron_string)); 
  57.         $start = emptyempty($_after_timestamp)?time():$_after_timestamp; 
  58.  
  59.         $date = array( 'minutes' =>self::_parseCronNumbers($cron[0],0,59), 
  60.                             'hours' =>self::_parseCronNumbers($cron[1],0,23), 
  61.                             'dom' =>self::_parseCronNumbers($cron[2],1,31), 
  62.                             'month' =>self::_parseCronNumbers($cron[3],1,12), 
  63.                             'dow' =>self::_parseCronNumbers($cron[4],0,6), 
  64.                         ); 
  65.         // limited to time()+366 - no need to check more than 1year ahead 
  66.         for($i=0;$i<=60*60*24*366;$i+=60){ 
  67.             if( in_array(intval(date('j',$start+$i)),$date['dom']) && 
  68.                 in_array(intval(date('n',$start+$i)),$date['month']) && 
  69.                 in_array(intval(date('w',$start+$i)),$date['dow']) && 
  70.                 in_array(intval(date('G',$start+$i)),$date['hours']) && 
  71.                 in_array(intval(date('i',$start+$i)),$date['minutes']) 
  72.  
  73.                 ){ 
  74.                     return $start+$i; 
  75.             } 
  76.         } 
  77.         return null; 
  78.     } 
  79.  
  80.     /** 
  81.  * get a single cron style notation and parse it into numeric value 
  82.  * 
  83.  * @param string $s cron string element 
  84.  * @param int $min minimum possible value 
  85.  * @param int $max maximum possible value 
  86.  * @return int parsed number 
  87.  */ 
  88.     protected static function _parseCronNumbers($s,$min,$max) 
  89.     { 
  90.         $result = array(); 
  91.  
  92.         $v = explode(',',$s); 
  93.         foreach($v as $vv){ 
  94.             $vvv = explode('/',$vv); 
  95.             $step = emptyempty($vvv[1])?1:$vvv[1]; 
  96.             $vvvv = explode('-',$vvv[0]); 
  97.             $_min = count($vvvv)==2?$vvvv[0]:($vvv[0]=='*'?$min:$vvv[0]); 
  98.             $_max = count($vvvv)==2?$vvvv[1]:($vvv[0]=='*'?$max:$vvv[0]); 
  99.  
  100.             for($i=$_min;$i<=$_max;$i+=$step){ 
  101.                 $result[$i]=intval($i); 
  102.             } 
  103.         } 
  104.         ksort($result); 
  105.         return $result; 
  106.     } 
  107. }

Tags: crontab

分享到: