这篇文章主要介绍了php版本的cron定时任务执行器使用实例,是非常实用的一个功能,需要的朋友可以参考下
本文实例讲述了php版本的cron定时任务执行器使用方法,是非常实用的一个功能应用。具体方法如下:
由于服务器crontab只能精确到分钟,因此程序的起点也是分钟。
该功能一共包括三个部分:
一、配置文件:
配置文件是用来返回要执行的定时任务文件,注意一下*的使用就行了,有两个模式,就是
Y-m-d H:i :年 月 日 时 分
N H:i :星期(1 - 7|周一 - 周日) 时 分
配置文件croning.php如下:
/** * 任务管理器配置文件 * * Y-m-d H:i :年 月 日 时 分 * N H:i :星期(1 - 7|周一 - 周日) 时 分 * * 2013-12-25 19:49 : 固定时间,只执行一次 * *-12-25 20:00 : 每年的某月某日 某小时某分 * 2013-12-25 *:49 : 某天的每个小时的49分都执行一次 * *-*-* 20:00 : 每天晚上8点0分执行 * *-*-* *:* :每分钟都在执行 * 2 20:01 :每周二的20:01时间都执行一次 * * * 号表示当前位置的任何时间。以此类推.... * * 格式: * array( * key=>value, * ); * * 说明: * key是定义的执行时间,value是执行的文件,可以是数组或者字符串,当同一时间有多个任务执行时,为了避免key的覆盖请用一维数组模式。 * */ 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') );
二、服务器cronjob主要执行的php文件:
该php文件主要处理与分析哪些文件是当时可以执行的。以及写入执行记录文件。
$__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)) {//是否是要执行的文件 $__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 . ' 执行开始----------' . date('Y-m-d H:i:s') . '-----------', $__echo); include ONING_ROOT . '/' . $__value; Common::fileLog(DATA_ROOT . '/log/cron_index.log', $__value . ' 执行结束(花费时间:' . ((microtime(true) - $__time_star2) * 1000) . 'ms)-------------', $__echo); } } } Common::fileLog(DATA_ROOT . '/log/cron_index.log', '执行cron结束(一共执行时间:' . ((microtime(true) - $__time_star) * 1000) . 'ms)*************' . date('Y-m-d H:i:s') . '*****************' . "\n\n", $__echo); /** * 处理正则结果并返回该文件是否是当时要执行 * @param array $match 正则结果,数组 * @param integer $__now 当时时间戳 * @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; }
三、众多要执行的定时文件:
这个是真正要执行的代码:包括采集,数据整理与分析等,文件路径写到配置文件的value中去。同一时间执行的文件,记得一维数组模式。
感兴趣的朋友可以调试运行一下本文实例程序,相信会有很大的收获。
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
