【问题标题】:Setting up a cron job in drupal 7在 drupal 7 中设置 cron 作业
【发布时间】:2019-01-13 19:22:59
【问题描述】:

我是 drupal 的新手,我的任务是设置一个每小时运行的 cron 作业。我有一个生成 xml 文件的 php 文件,该文件将在不同的站点上使用。

我的问题是:我是否将 mycron.php 放在根目录中(与 cron.php 相同)并将 crontab 配置为每小时运行一次 mycron.php?

感谢任何指导。

【问题讨论】:

    标签: drupal cron drupal-7


    【解决方案1】:

    您可以在自定义模块中使用 hook_cron() 编写自己的 cron 作业,并使用项目模块 Elysia Cron 对其进行设置,以获取每个 cron 任务的时间和频率。

    【讨论】:

      【解决方案2】:

      最初我带着一个类似的问题来到这个页面。这基本上是我发现的。

      您不会从 PHP 代码运行 cron 作业,而是从服务器操作系统运行 cron 作业。 Cron 作业只能在 Linux、Unix 或 macOS 中设置,windows 没有预装的 cron 系统。

      如果您使用 VPS,您可以从您的操作系统(例如ubuntu)设置 cron 作业。或者,如果您使用的是共享主机,您很可能能够从您帐户的管理菜单中设置您的 cron 作业,这取决于您的主机提供商。

      您所做的是在您的 Drupal 模块 hook_menu 中创建一个端点。菜单中的端点应链接到一个回调函数,该函数将执行您希望定期运行的操作。

      function module_name_menu() {
        return [
          'path/to/endpoint/%' => [
            'title'            => t(Menu title), 
            'description'      => 'Some description',
            'page callback'    => 'name_of_function_to_call',
            // Optional argument passed to the callback function, number relates to the position in the path
            'page arguments'   => [3], 
            'access arguments' => ['type of access'],
            'type'             => MENU_CALLBACK,
          ]
        ];
      }
      

      检查 hook_menu 链接,看看函数返回数组中的元素做了什么。

      /**
       * Cron job callback function
       * @param string $param Parameter sent through the url
       */
      function name_of_function_to_call($param) {
        // Do something with the param and perform some tasks
      }
      

      在您要设置的 cron 作业中,您必须将您的 cron 作业指向端点位置。下面示例中的 cron 作业将在 1 月的每个第一天和 1 月的每个星期一(分钟、小时、月中的某天、月、周中的某天)的 4 点后一分钟运行:

      01 04 1 1 1 wget -O - -q -t 1 http://siteurl.tld/path/to/endpoint/argument
      

      (来自Drupal documentation 的示例,执行man wget 以了解 wget 选项的作用)

      编辑:您显然也有hook_cron 选项。您将代码放在 ..._cron() {} 函数中的位置,该函数将在页面范围的 cron 作业运行时运行,但不会让您有很多控制权。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        • 2012-11-12
        • 2015-09-18
        • 2018-04-09
        • 2015-06-07
        • 1970-01-01
        相关资源
        最近更新 更多