本文實例講述了php版本的cron定時任務(wù)執(zhí)行器使用方法,是非常實用的一個功能應(yīng)用。具體方法如下:
由于服務(wù)器crontab只能精確到分鐘,因此程序的起點也是分鐘。
該功能一共包括三個部分:
一、配置文件:
配置文件是用來返回要執(zhí)行的定時任務(wù)文件,注意一下*的使用就行了,有兩個模式,就是
Y-m-d H:i :年 月 日 時 分
N H:i :星期(1 - 7|周一 - 周日) 時 分
配置文件croning.php如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/** * 任務(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點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í)行記錄文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
<?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é)束(花費時間:' . ((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)試運行一下本文實例程序,相信會有很大的收獲。