2

只是想知道是否有人对在 Expression Engine 中运行的脚本设置 cron 作业的最佳方法有任何建议。

目前我的计划是使用 cron 作业通过 Lynx 访问 URL。URL 将是一个随机字符串,因此不会被偶然发现,但可以公开访问。cron 作业将加载 URL,脚本将作为表达式引擎的一部分运行。

运行这些脚本的理想方法似乎是让一个 cron 作业在内部运行 PHP 脚本,但此时我需要它从 EE 框架内运行东西,所以调用我的模块脚本会失败,因为它不会t被管道输入。

我怎么能通过管道来工作,或者我应该只使用 A 计划?

4

2 回答 2

2

EE2 论坛EE1 论坛上,您会发现很多人使用 cron 作业来处理各种事情。

最流行的用途似乎是自动关闭过期条目,使用带有预定 cron 作业的命令行 PHP 脚本:

#!usr/local/bin/php -n

<?php global $PREFS;                            // ee_cli_sql.php v0.3.5

/* Copyright (c) 2003 - 2007 EllisLab, Inc.  ---  ExpressionEngine 1.6.0.

   Some code by Ingmar Greil, 2007. <office@ingmar.at>
   My modfications and code released under a Creative Commons
   "Attribution" license: http://creativecommons.org/licenses/by/2.0/

   This PHP command line script allows you to perform arbitrary
   SQL queries on your EE database. There will be no visible output
   (in this case we'd simply use the Query module in a template, right?),
   since the whole point is to run this script unattended.

   Put this file in your EE "system" folder, make sure the executable
   bit is set (chmod +x ee_cli_sql.php), then call manually or via cron.

   Try "crontab -e".
   "5 * * * * command" will run your script 5 minutes past the hour, every hour.
   "0,10,20,30,40,50 6-22 * * * 1-5" will run your script every ten minutes
   between 6am and 10pm, except on weekends. The general syntax is:
   <Minute> <Hour> <Day> <Month> <Day of Week> <Command line>

*/

// This query will set all expired entries to "closed" status:

$query = "UPDATE `exp_weblog_titles` SET status = 'closed' WHERE status <> 'closed'
          AND expiration_date <> '0' AND expiration_date < UNIX_TIMESTAMP()";

// Change the above query to suit your needs.
// That's it, folks! No user-serviceable parts below.

define("EXT",".php");                           // Get around EE's security mechanisms. Kludgy? Hell, yes.
                                                // Got a better solution? I am all ears.
require("config".EXT);                          // Read the config file
require("core/core.prefs".EXT);                 // Load the PREFS cass

$PREFS = new Preferences(); $PREFS->core_ini = $conf; unset($conf);

$db = mysql_connect(                            // Handle the connection to the database:
      $PREFS->core_ini['db_hostname'],          // hostname,
      $PREFS->core_ini['db_username'],          // username and
      $PREFS->core_ini['db_password']);         // password are all pulled automatically.

mysql_select_db($PREFS->core_ini['db_name']);   // Now it's selecting the appropriate db,
mysql_query($query,$db);                        // performing the actual query, then
mysql_close();                                  // cleaning up after ourselves. Done.
?>

ExpressionEngine Wiki 文章提供了一些关于如何设置和安排脚本的见解:

这个 PHP 命令行脚本允许您在 EE 数据库上执行任意 SQL 查询。不会有可见的输出(在这种情况下,我们只需在模板中使用 Query 模块,对吗?),因为重点是在无人看管的情况下运行此脚本。

将此文件放入您的 EE“系统”文件夹中,确保设置了可执行位(chmod +x ee_cli_sql.php),然后手动或通过 cron 调用。

如果您不想使用 CLI(命令行界面)来管理您的 cron 作业,您可以考虑 EllisLab 的Cron 插件,它可以设置为定期调用插件或模块。

于 2011-05-26T17:20:44.517 回答
0

我认为最简单的选择就是将执行此操作的 PHP 脚本分开。

但是,要在 EE 中完成此操作,您需要创建一个插件或扩展程序(我永远不记得哪个应该执行什么任务)执行您想要运行的任何 PHP 代码,然后从任何地方调用扩展程序或插件您创建的通过 URL 访问的模板页面。

即标注将类似于:

{exp:runcron}
{/exp:runcron}

该代码将调用插件,并且插件将运行 PHP 代码来执行您想到的任何任务。

于 2011-03-08T15:29:49.537 回答