本文实例讲述了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中去。同一时间执行的文件,记得一维数组模式。
感兴趣的朋友可以调试运行一下本文实例程序,相信会有很大的收获。

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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