一个用于多个子域的cronjob

时间:2017-03-27 16:09:35

标签: php cron include

我在根目录中设置了一个cronjob'cron_parent.php'。它的工作原理如下:

  1. 检查存在哪些客户文件夹(即子域名)
  2. 使用foreach(),为每个相关子域包含cron_child.php
  3. 实际工作在每个子域的cron_child.php中完成。
  4. 所有子域都包含相同的php文件和功能。当然,我在多个子域上使用它时遇到了麻烦,因为

    <b>Fatal error</b>:  Cannot redeclare fserror() (previously declared in
    /home/***hidden***/public_html/demo/dbconfig.php:28) 
    in <b>/home/***hidden***/public_html/dev/dbconfig.php</b> on line <b>44</b><br />
    

    我意识到include()可能不是正确的选择。有没有办法从cron_parent.php运行cron_child.php的'detached'?

    编辑:添加了来自cron_parent.php的代码:

    <?php
    date_default_timezone_set("Europe/Oslo");
    header('Content-Type: text/html; charset=utf-8');
    
    $root = dirname(dirname(__FILE__));
    $directories = glob($root . '/*' , GLOB_ONLYDIR);
    
    foreach($directories as $path)
    {
      if(is_file("$path/cron_child.php"))
      {
        include("$path/cron_child.php");
      }
    }
    

    出于测试目的,cron_child.php现在只包含此内容:

    <?php
    include "functions.php";
    return false;
    

1 个答案:

答案 0 :(得分:2)

阅读DirectoryIteratorClasses

这样的东西应该起作用(只是结构):

// main.php
<?php

    require_once('jobHandler.php');

    foreach (new DirectoryIterator('your/folder') as $fileInfo) {
        // If it is not folder skip;
        if($fileInfo->isDot() || ! $file->isDir) continue;

        $worker = new jobHandler($folderToProcess);
        $worker->run();
    }


// jobHandler.php    
<?php

    class jobHandler
    {
        public function __construct($folder)
        {
            // validation here
        }

        public function run()
        {
            echo $this->folder . PHP_EOL;
            // Do your work here.
        }

        private $folder = null;        
    }

您可以将作业配置存储在客户文件夹中。

相关问题