Implement crontab code sharing in PHP, Phpcrontab code sharing _php Tutorials

Source: Internet
Author: User
Tags php source code crontab syntax

Implement crontab code sharing in PHP, Phpcrontab code sharing


1. Prepare a standard crontab file./crontab
Copy the Code code as follows:
# m H Dom Mon Dow command
* * * * * date >/tmp/cron.date.run

2. Crontab-e this cron.php script into the system cron

Copy the Code code as follows:
* * * * * */usr/bin/php cron.php

3. cron.php Source Code

Copy the Code code 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/974679.html www.bkjia.com true http://www.bkjia.com/PHPjc/974679.html techarticle implement crontab code sharing in PHP, Phpcrontab code sharing 1. Prepare a standard crontab file./crontab Copy the code code as follows: # m H dom Mon Dow Command * * * * * date/tmp /cron ....

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.