PHP中實(shí)現(xiàn)crontab代碼分享
來(lái)源:易賢網(wǎng) 閱讀:824 次 日期:2015-03-30 11:46:17
溫馨提示:易賢網(wǎng)小編為您整理了“PHP中實(shí)現(xiàn)crontab代碼分享”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了PHP中實(shí)現(xiàn)crontab代碼分享,本文給出了實(shí)現(xiàn)代碼和使用方法,需要的朋友可以參考下

1. 準(zhǔn)備一個(gè)標(biāo)準(zhǔn)crontab文件 ./crontab

代碼如下:

# m h dom mon dow command

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

2. crontab -e 將此cron.php腳本加入系統(tǒng)cron

代碼如下:

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

3. cron.php 源碼

代碼如下:

// 從./crontab讀取cron項(xiàng),也可以從其他持久存儲(chǔ)(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::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;

}

}

/* */

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],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(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;

}

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:PHP中實(shí)現(xiàn)crontab代碼分享
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)