Example of cron scheduled task executor in php

Source: Internet
Author: User
This article describes how to use cron scheduled task executors in php. It is a very practical function. For more information, see

This article describes how to use cron scheduled task executors in php. It is a very practical function. For more information, see

This article describes how to use cron scheduled task executors in php. It is a very practical function application. The specific method is as follows:

Since the crontab of the server can only be accurate to minutes, the starting point of the program is also minutes.

This function consists of three parts:

1. configuration file:

The configuration file is used to return the scheduled task file to be executed. Pay attention to the usage of *. There are two modes:

Y-m-d H: I: Mm-dd
N h: I: hour of the Week (1-7 | Monday-Sunday)

The configuration file croning. php is as follows:

/*** Task Manager Configuration File ** Y-m-d H: I: mm/DD * n h: I: Week (1-7 | Monday-Sunday) hour **: fixed time, only one time **-12-25: hour of a month/day of each year *: 49: run **-* 20:00: Execute **-*-**: * every minute at 08:00*2 20: 01: Execute *** at every Tuesday to indicate any time at the current position. And so on .... ** format: * array (* key => value, *); ** Note: * key indicates the execution time defined. value indicates the file to be executed, which can be an array or a string, when multiple tasks are executed at the same time, use the one-dimensional array mode to avoid overwriting keys. **/Return array ('2017-12-25 '=> '2017. php', '2017-12-* '=> '2017. php ', '1' => '6546. php ',' *-12-25 '=> array ('2017. php', '2017. php '));

Ii. Main PHP files executed by the cronjob Server:

This PHP file mainly processes and analyzes which files can be executed at the time. And write the execution record file.

<? Php/*** the file that the cron task executes in a unified manner and does not time out */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'); // the regular execution file directory require CORE_ROOT. '/Common. php '; require CORE_ROOT. '/AHA. php '; // load the framework core file spl_autoload_register (array ('common', 'loadclassfile'); AHA: initConfig (include APP_ROOT. '/_ config/inc. php '); // load the configuration file // if (! File_exists (APP_ROOT. '/_ config/croning. php') {exit ('cron failed, please check the cron config! ') ;}__ __All = include APP_ROOT.'/_ config/croning. php'; // if (! $ __All |! Is_array ($ __all) {exit ('cron failed, please check the cron config! ') ;}__ __Echo = true; // whether to output to the screen $ __time_star = microtime (true); $ __now = time (); Common: fileLog (DATA_ROOT. '/log/cron_index.log ', 'Start cron ******************************'. date ('Y-m-d H: I: s', $ __now ). ***************************** ', $ __echo ); $ __onfile = array (); if ($ __all) {foreach ($ __all as $ __key = >__ __value) {if (strpos ($ __key, '-') = false) {// weekly processing of preg_match ('@ ^ ([\ d \ *] +) ([\ d \ *] +) :( [\ d \ *] +) $ @ U', $ __key, $ match);} else {// normal processing of preg_match ('@ ^ ([\ d \ *] +) \-([\ d \ *] +) \-([\ d \ *] +) ([\ d \ *] +) :( [\ d \ *] +) $ @ U', $ __key, $ match);} if ($ match) {array_shift ($ match); if (_ getPreg ($ match, $ __now )) {// is the file to be executed $ __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. 'execution started ----------'. date ('Y-m-d H: I: s '). '-----------', $ __echo); include ONING_ROOT. '/'. $ __value; Common: fileLog (DATA_ROOT. '/log/cro N_index.log ', $ __value. 'execution ended (time spent :'. (microtime (true)-$__time_star2) * 1000 ). 'Ms) ------------- ', $ __echo) ;}} Common: fileLog (DATA_ROOT. '/log/cron_index.log', 'execution cron ends (total execution time :'. (microtime (true)-$ __time_star) * 1000 ). 'Ms )*************'. date ('Y-m-d H: I: s '). '*****************'. "\ n", $ __echo);/*** process the regular result and return whether the file was to be executed * @ param array $ match the regular result, array * @ param integer $ __now Timestamp * @ 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 ;}

3. Many scheduled files to be executed:

This is the real code to be executed, including collection, data sorting and analysis, and the file path is written to the value of the configuration file. For Files executed at the same time, remember the one-dimensional array mode.

If you are interested, you can debug and run the instance program in this article. I believe it will be a great achievement.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.