php版本的cron定时任务执行器使用实例_php技巧
本文实例讲述了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文件主要处理与分析哪些文件是当时可以执行的。以及写入执行记录文件。
<?php /** * cron任务统一执行的文件,没有超时 */ 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'); //定时执行文件目录 require CORE_ROOT . '/Common.php'; require CORE_ROOT . '/AHA.php'; //载入框架核心文件 spl_autoload_register(array('Common', 'loadClassFile')); AHA::initConfig(include APP_ROOT . '/_config/inc.php'); //载入配置文件 //不存在执行的配置文件时 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; //是否输出到屏幕 $__time_star = microtime(true); $__now = time(); Common::fileLog(DATA_ROOT . '/log/cron_index.log', '执行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)) {//是否是要执行的文件 $__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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
