![PHP中實現(xiàn)crontab代碼分享__第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/3f024223-4410-486a-b390-1624e9692969/3f024223-4410-486a-b390-1624e96929691.gif)
![PHP中實現(xiàn)crontab代碼分享__第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/3f024223-4410-486a-b390-1624e9692969/3f024223-4410-486a-b390-1624e96929692.gif)
![PHP中實現(xiàn)crontab代碼分享__第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/3f024223-4410-486a-b390-1624e9692969/3f024223-4410-486a-b390-1624e96929693.gif)
![PHP中實現(xiàn)crontab代碼分享__第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/3f024223-4410-486a-b390-1624e9692969/3f024223-4410-486a-b390-1624e96929694.gif)
![PHP中實現(xiàn)crontab代碼分享__第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/3f024223-4410-486a-b390-1624e9692969/3f024223-4410-486a-b390-1624e96929695.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、PHP中實現(xiàn)crontab代碼分享_ 這篇文章主要介紹了PHP中實現(xiàn)crontab代碼分享,本文給出了實現(xiàn)代碼和用法方法,需要的伴侶可以參考下 1. 預(yù)備一個標(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項,也可以從其他長久存儲(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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年企業(yè)機(jī)器質(zhì)押借款合同
- 2025年勞動解除合同標(biāo)準(zhǔn)條款
- 2025年抗瘧藥項目申請報告模范
- 2025年貨車租賃與運(yùn)輸服務(wù)合同樣本
- 2025年國際貨物買賣合同與慣例
- 2025年專業(yè)清潔人員派遣協(xié)議
- 2025年二手車購買合同范本
- 2025年三板市場股權(quán)買賣協(xié)議
- 2025年伙伴開設(shè)教育機(jī)構(gòu)合作協(xié)議書模板
- 2025年繼電器研發(fā)策劃技術(shù)協(xié)議書范本
- 部編六年級下冊語文《1 北京的春節(jié)》課件
- 駕駛員安全行車考核獎懲制度(3篇)
- 2024屆安徽省普通高校分類考試招生和對口招生文化素質(zhì)語文模擬檢測試題(含答案)
- 籃球俱樂部合伙協(xié)議
- 中學(xué)學(xué)校2024-2025學(xué)年教學(xué)專項發(fā)展規(guī)劃
- 臨時道路鋪設(shè)鋼板施工方案
- 屋頂光伏工程施工方案
- 家長會課件:小學(xué)三年級家長會 課件
- 電力基建復(fù)工安全教育培訓(xùn)
- 勞務(wù)經(jīng)紀(jì)人培訓(xùn)
- 歐洲電力回顧2024(英)
評論
0/150
提交評論