Implementing crontab code sharing in PHP
This article mainly introduces the implementation of PHP crontab code sharing, this article gives the implementation of code and use of methods, the need for friends can refer to the following
1. Prepare a standard crontab file./crontab
The code is as follows:
# m H Dom Mon Dow command
* * * * * date >/tmp/cron.date.run
2. Crontab-e this cron.php script into the system cron
The code is as follows:
* * * * * */usr/bin/php cron.php
3. cron.php Source Code
The code is as follows:
Read cron items from./crontab, or read from other persistent storage (MySQL, Redis)
$crontab = File ('./crontab ');
$now = $_server[' request_time ');
foreach ($crontab as $cron) {
$slices = Preg_split ("/[\s]+/", $cron, 6);
if (count ($slices)!== 6) continue;
$cmd = Array_pop ($slices);
$cron _time = Implode (", $slices);
$next _time = Crontab::p arse ($cron _time, $now);
if ($next _time!== $now) continue;
$pid = Pcntl_fork ();
if ($pid = =-1) {
Die (' could not fork ');
} else if ($pid) {
We are the parent
Pcntl_wait ($status, Wnohang); Protect against Zombie Children
} else {
We are the child
' $cmd ';
Exit
}
}
/* Https://github.com/jkonieczny/PHP-Crontab */
Class Crontab {
/**
* Finds Next Execution Time (stamp) Parsin crontab syntax,
* After given starting timestamp (or current time if ommited)
*
* @param string $_cron_string:
*
* 0 1 2 3 4
* * * * * *
* - - - - -
* | | | | |
* | | | | +-----Day of Week (0-6) (sunday=0)
* | | | +-------Month (1-12)
* | | +---------Day of month (1-31)
* | +-----------Hour (0-23)
* +-------------min (0-59)
* @param int $_after_timestamp timestamp [default=current timestamp]
* @return int Unix Timestamp-next execution time would be greater
* Than given timestamp (defaults to the current timestamp)
* @throws InvalidArgumentException
*/
public static function Parse ($_cron_string,$_after_timestamp=null)
{
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))) {
throw new InvalidArgumentException ("Invalid cron string:". $_cron_string);
}
if ($_after_timestamp &&!is_numeric ($_after_timestamp)) {
throw new InvalidArgumentException ("\$_after_timestamp must be a valid UNIX timestamp ($_after_timestamp given)");
}
$cron = Preg_split ("/[\s]+/i", Trim ($_cron_string));
$start = Empty ($_after_timestamp)? Time (): $_after_timestamp;
$date = Array (' minutes ' =>self::_parsecronnumbers ($cron [0],0,59),
' Hours ' =>self::_parsecronnumbers ($cron [1],0,23),
' Dom ' =>self::_parsecronnumbers ($cron [2],1,31),
' Month ' =>self::_parsecronnumbers ($cron [3],1,12),
' Dow ' =>self::_parsecronnumbers ($cron [4],0,6),
);
Limited to time () +366-no need to check more than 1year ahead
for ($i =0; $i <=60*60*24*366; $i +=60) {
if (In_array (Intval (' J ', $start + $i), $date [' Dom ']) &&
In_array (intval (Date (' n ', $start + $i)), $date [' Month ']) &&
In_array (intval (Date (' W ', $start + $i)), $date [' Dow ']) &&
In_array (intval (Date (' G ', $start + $i)), $date [' hours ']) &&
In_array (intval (Date (' I ', $start + $i)), $date [' minutes '])
){
return $start + $i;
}
}
return null;
}
/**
* Get a single cron style notation and parse it into numeric value
*
* @param string $s cron string element
* @param int $min minimum possible value
* @param int $max maximum possible value
* @return int parsed number
*/
protected static function _parsecronnumbers ($s, $min, $max)
{
$result = Array ();
$v = Explode (', ', $s);
foreach ($v as $VV) {
$VVV = explode ('/', $VV);
$step = Empty ($VVV [1])? 1: $VVV [1];
$VVVV = Explode ('-', $VVV [0]);
$_min = count ($VVVV) ==2? $VVVV [0]:($VVV [0]== ' * '? $min: $VVV [0]);
$_max = count ($VVVV) ==2? $VVVV [1]:($VVV [0]== ' * '? $max: $VVV [0]);
for ($i =$_min; $i <=$_max; $i + = $step) {
$result [$i]=intval ($i);
}
}
Ksort ($result);
return $result;
}
}
http://www.bkjia.com/PHPjc/974521.html www.bkjia.com true http://www.bkjia.com/PHPjc/974521.html techarticle implementing crontab code sharing in PHP this article mainly introduces the implementation of Crontab code sharing in PHP, this article gives the implementation code and use of the method, the need for friends can refer to the next 1. Prepare a standard ...