亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

php語(yǔ)言

PHP中如何實(shí)現(xiàn)crontab代碼

時(shí)間:2024-05-30 22:56:38 php語(yǔ)言 我要投稿
  • 相關(guān)推薦

PHP中如何實(shí)現(xiàn)crontab代碼

  PHP是一種通用開(kāi)源腳本語(yǔ)言。語(yǔ)法吸收了C語(yǔ)言、Java和Perl的特點(diǎn),利于學(xué)習(xí),使用廣泛,主要適用于Web開(kāi)發(fā)領(lǐng)域。下面,小編為大家搜索整理了PHP中如何實(shí)現(xiàn)crontab代碼,希望能給大家?guī)?lái)幫助!更多精彩內(nèi)容請(qǐng)及時(shí)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  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;

  }

  }

  /* 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],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;

  }

  }

【PHP中如何實(shí)現(xiàn)crontab代碼】相關(guān)文章:

PHP中多態(tài)如何實(shí)現(xiàn)09-04

PHP實(shí)現(xiàn)大文件上傳源代碼10-21

PHP中實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)07-06

php如何實(shí)現(xiàn)驗(yàn)證碼06-13

php語(yǔ)言字典代碼06-08

PHP如何使用curl實(shí)現(xiàn)數(shù)據(jù)抓取09-27

如何利用PHP時(shí)間轉(zhuǎn)換Unix時(shí)間戳代碼10-21

php中怎么實(shí)現(xiàn)回刪功能08-31

php中實(shí)現(xiàn)回刪功能實(shí)例10-03

如何實(shí)現(xiàn)PHP獲取表單數(shù)據(jù)與HTML嵌入PHP腳本09-23