php版本的cron定时任务执行器使用实例
author:一佰互联 2019-04-29   click:204

本文实例讲述了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") . "*****************" . "

", $__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中去。同一时间执行的文件,记得一维数组模式。

感兴趣的朋友可以调试运行一下本文实例程序,相信会有很大的收获。