Share crontab code in PHP

Source: Internet
Author: User
Tags crontab syntax

Share crontab code in PHP

This article mainly introduces how to implement crontab code sharing in PHP. This article provides the implementation code and how to use it. For more information, see

 

 

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 Add the cron. php script to 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 entries from./crontab or from other persistent storage (mysql or 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: parse ($ 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 will 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 ),
'Hours' => self: _ parseCronNumbers ($ cron [1 ),
'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 1 year ahead
For ($ I = 0; $ I <= 60*60*24*366; $ I + = 60 ){
If (in_array (intval (date ('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;
}
}

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.