PHP中實(shí)現(xiàn)crontab代碼分享__第1頁(yè)
PHP中實(shí)現(xiàn)crontab代碼分享__第2頁(yè)
PHP中實(shí)現(xiàn)crontab代碼分享__第3頁(yè)
PHP中實(shí)現(xiàn)crontab代碼分享__第4頁(yè)
PHP中實(shí)現(xiàn)crontab代碼分享__第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、PHP中實(shí)現(xiàn)crontab代碼分享_ 這篇文章主要介紹了PHP中實(shí)現(xiàn)crontab代碼分享,本文給出了實(shí)現(xiàn)代碼和用法方法,需要的伴侶可以參考下 1. 預(yù)備一個(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),也可以從其他長(zhǎng)久存儲(chǔ)(mysql、re

2、dis)讀取 $crontab = file(./crontab); $now = $_SERVERREQUEST_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 )

3、 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 sta

4、rting 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 tim

5、estamp 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+

6、(*(/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 va

7、lid 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($cron0,0,59), hours =self:_parseCronNumbers($cron1,0,23), dom =self:_parseCronNumbers($cron2,1,31), m

8、onth =self:_parseCronNumbers($cron3,1,12), dow =self:_parseCronNumbers($cron4,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),$datedom) in_array(intval(date(n,$start+$i),$datemonth) in_array(intval(date(

9、w,$start+$i),$datedow) in_array(intval(date(G,$start+$i),$datehours) in_array(intval(date(i,$start+$i),$dateminutes) ) 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

10、* 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($vvv1)?1:$vvv1; $vvvv = explode(-,$vvv0); $_min = count($vvvv)=2?$vvvv0:($vvv0=*?$min:$vvv0

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論