在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 插件,它可以设置为定期调用插件或模块。