php版本的cron定時任務(wù)執(zhí)行器使用實(shí)例
本文實(shí)例講述了php版本的cron定時任務(wù)執(zhí)行器使用方法,是非常實(shí)用的一個功能應(yīng)用。具體方法如下:
由于服務(wù)器crontab只能精確到分鐘,因此程序的起點(diǎn)也是分鐘。
該功能一共包括三個部分:
一、配置文件:
配置文件是用來返回要執(zhí)行的定時任務(wù)文件,注意一下*的使用就行了,有兩個模式,就是
Y-m-d H:i :年 月 日 時 分
N H:i :星期(1 - 7|周一 - 周日) 時 分
配置文件croning.php如下:
/** * 任務(wù)管理器配置文件 * * Y-m-d H:i :年 月 日 時 分 * N H:i :星期(1 - 7|周一 - 周日) 時 分 * * 2013-12-25 19:49 : 固定時間,只執(zhí)行一次 * *-12-25 20:00 : 每年的某月某日 某小時某分 * 2013-12-25 *:49 : 某天的每個小時的49分都執(zhí)行一次 * *-*-* 20:00 : 每天晚上8點(diǎn)0分執(zhí)行 * *-*-* *:* :每分鐘都在執(zhí)行 * 2 20:01 :每周二的20:01時間都執(zhí)行一次 * * * 號表示當(dāng)前位置的任何時間。以此類推.... * * 格式: * array( * key=>value, * ); * * 說明: * key是定義的執(zhí)行時間,value是執(zhí)行的文件,可以是數(shù)組或者字符串,當(dāng)同一時間有多個任務(wù)執(zhí)行時,為了避免key的覆蓋請用一維數(shù)組模式。 * */ return array( '2013-12-25 19:49'=>'123.php', '2013-12-* 18:00'=>'456.php', '1 08:00'=>'6546.php', '*-12-25 19:49'=>array('444.php','456.php') );
二、服務(wù)器cronjob主要執(zhí)行的php文件:
該php文件主要處理與分析哪些文件是當(dāng)時可以執(zhí)行的。以及寫入執(zhí)行記錄文件。
<?php /** * cron任務(wù)統(tǒng)一執(zhí)行的文件,沒有超時 */ header('Content-Type:text/html; charset=utf-8'); set_time_limit(0); define('APP_ROOT', dirname(__FILE__)); define('AHA_ROOT', dirname(APP_ROOT)); define('CORE_ROOT', AHA_ROOT . '/__core'); define('DATA_ROOT', AHA_ROOT . '/data'); define('MODEL_ROOT', APP_ROOT . '/model'); define('ONING_ROOT', APP_ROOT . '/oning'); //定時執(zhí)行文件目錄 require CORE_ROOT . '/Common.php'; require CORE_ROOT . '/AHA.php'; //載入框架核心文件 spl_autoload_register(array('Common', 'loadClassFile')); AHA::initConfig(include APP_ROOT . '/_config/inc.php'); //載入配置文件 //不存在執(zhí)行的配置文件時 if (!file_exists(APP_ROOT . '/_config/croning.php')) { exit('cron failed,please check the cron config!'); } $__all = include APP_ROOT . '/_config/croning.php'; //數(shù)據(jù)不合法時 if (!$__all || !is_array($__all)) { exit('cron failed,please check the cron config!'); } $__echo = true; //是否輸出到屏幕 $__time_star = microtime(true); $__now = time(); Common::fileLog(DATA_ROOT . '/log/cron_index.log', '執(zhí)行cron開始******************************' . date('Y-m-d H:i:s', $__now) . '******************************', $__echo); $__onFile = array(); if ($__all) { foreach ($__all as $__key => $__value) { if (strpos($__key, '-') === false) {//每周的處理 preg_match('@^([\d\*]+) ([\d\*]+):([\d\*]+)$@U', $__key, $match); } else {//正常的處理 preg_match('@^([\d\*]+)\-([\d\*]+)\-([\d\*]+) ([\d\*]+):([\d\*]+)$@U', $__key, $match); } if ($match) { array_shift($match); if (__getPreg($match, $__now)) {//是否是要執(zhí)行的文件 $__onFile = array_merge($__onFile, is_array($__value) ? $__value : array($__value)); } } } } if ($__onFile) { $__onFile = array_unique($__onFile); foreach ($__onFile as $__value) { if (file_exists(ONING_ROOT . '/' . $__value)) { $__time_star2 = microtime(true); Common::fileLog(DATA_ROOT . '/log/cron_index.log', $__value . ' 執(zhí)行開始----------' . date('Y-m-d H:i:s') . '-----------', $__echo); include ONING_ROOT . '/' . $__value; Common::fileLog(DATA_ROOT . '/log/cron_index.log', $__value . ' 執(zhí)行結(jié)束(花費(fèi)時間:' . ((microtime(true) - $__time_star2) * 1000) . 'ms)-------------', $__echo); } } } Common::fileLog(DATA_ROOT . '/log/cron_index.log', '執(zhí)行cron結(jié)束(一共執(zhí)行時間:' . ((microtime(true) - $__time_star) * 1000) . 'ms)*************' . date('Y-m-d H:i:s') . '*****************' . "\n\n", $__echo); /** * 處理正則結(jié)果并返回該文件是否是當(dāng)時要執(zhí)行 * @param array $match 正則結(jié)果,數(shù)組 * @param integer $__now 當(dāng)時時間戳 * @return bool */ function __getPreg($match, $__now) { $back = false; list($__Y, $__m, $__d, $__N, $__H, $__i) = explode('-', date('Y-m-d-N-H-i', $__now)); $argc = count($match); if ($argc === 3) { $argc = $match[0] === '*' ? $__N : $match[0]; $argc.=' '; $argc.=$match[1] === '*' ? $__H : $match[1]; $argc.=':'; $argc.=$match[2] === '*' ? $__i : $match[2]; $back = date('N H:i', $__now) === date($argc, $__now) ? true : false; } elseif ($argc === 5) { $argc = $match[0] === '*' ? $__Y : $match[0]; $argc.='-'; $argc.=$match[1] === '*' ? $__m : $match[1]; $argc.='-'; $argc.=$match[2] === '*' ? $__d : $match[2]; $argc.=' '; $argc.=$match[3] === '*' ? $__H : $match[3]; $argc.=':'; $argc.=$match[4] === '*' ? $__i : $match[4]; $back = date('Y-m-d H:i', $__now) === date($argc, $__now) ? true : false; } return $back; }
三、眾多要執(zhí)行的定時文件:
這個是真正要執(zhí)行的代碼:包括采集,數(shù)據(jù)整理與分析等,文件路徑寫到配置文件的value中去。同一時間執(zhí)行的文件,記得一維數(shù)組模式。
感興趣的朋友可以調(diào)試運(yùn)行一下本文實(shí)例程序,相信會有很大的收獲。
- 詳解PHP實(shí)現(xiàn)定時任務(wù)的五種方法
- 詳解PHP執(zhí)行定時任務(wù)的實(shí)現(xiàn)思路
- PHP定時執(zhí)行任務(wù)的3種方法詳解
- 如何使用純PHP實(shí)現(xiàn)定時器任務(wù)(Timer)
- PHP定時執(zhí)行任務(wù)實(shí)現(xiàn)方法詳解(Timer)
- php定時執(zhí)行任務(wù)設(shè)置詳解
- php計劃任務(wù)之ignore_user_abort函數(shù)實(shí)現(xiàn)方法
- PHP實(shí)現(xiàn)定時執(zhí)行任務(wù)的方法
- PHP中使用sleep函數(shù)實(shí)現(xiàn)定時任務(wù)實(shí)例分享
- 如何離線執(zhí)行php任務(wù)
相關(guān)文章
PHP常用字符串函數(shù)用法實(shí)例總結(jié)
這篇文章主要介紹了PHP常用字符串函數(shù)用法,結(jié)合實(shí)例形式總結(jié)分析了PHP常用字符串函數(shù)基本功能、用法及相關(guān)注意事項,需要的朋友可以參考下2020-06-06windows下PHP_intl.dll正確配置方法(apache2.2+php5.3.5)
配置php_intl模塊總是加載失敗,在這找到了解決方法2014-01-01php使用curl判斷網(wǎng)頁404(不存在)的方法
這篇文章主要介紹了php使用curl判斷網(wǎng)頁404(不存在)的方法,通過curl獲取http頭信息進(jìn)行404錯誤判斷,非常簡便易懂,需要的朋友可以參考下2016-06-06PHP實(shí)現(xiàn)過濾各種HTML標(biāo)簽
在做項目的過程中,我們經(jīng)常需要用到過濾一些html標(biāo)簽來實(shí)現(xiàn)提高數(shù)據(jù)的安全性,其實(shí)就是刪除那些對應(yīng)用程序有潛在危害的數(shù)據(jù)。它用于去除標(biāo)簽以及刪除或編碼不需要的字符。2015-05-05PHP實(shí)現(xiàn)的mysql讀寫分離操作示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的mysql讀寫分離操作,簡單講述了mysql讀寫分離的原理,并結(jié)合實(shí)例形式給出了php針對mysql的讀寫sql語句操作不同數(shù)據(jù)庫的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-05-05解析PHP留言本模塊主要功能的函數(shù)說明(代碼可實(shí)現(xiàn))
本篇文章是對PHP留言本中主要的函數(shù)以及代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06