带有Expression Engine脚本的Cron作业

时间:2011-02-28 18:13:05

标签: cron expressionengine

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

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

看起来运行这些脚本的理想方法是获取一个cron作业来在内部运行PHP脚本,但此时我需要它来从EE框架内运行,所以调用我的模块脚本会失败它不会被输入。

我怎样才能将其用于工作中,或者我应该选择A计划?

2 个答案:

答案 0 :(得分:2)

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

最流行的用法似乎是automatically closing expired entries,使用命令行PHP脚本和预定的cron作业:

#!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命令行脚本允许   您执行任意SQL查询   在你的EE数据库上。将没有   可见输出(在这种情况下我们是   只需使用查询模块即可   模板,对吗?),因为整体   要点是运行这个脚本   无人值守。

     

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

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

答案 1 :(得分:0)

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

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

即。标注类似于:

{exp:runcron}
{/exp:runcron}

该代码将调用该插件,该插件将运行PHP代码,执行您的任何任务。