2011-02-28 83 views
2

想知道是否有人对在Expression Engine中运行的脚本设置cron作业的最佳方法有任何建议。使用表达式引擎脚本的Cron作业

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

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

我怎样才能管这工作,或者我应该与计划A?

回答

0

我认为最简单的选择只是让PHP脚本分开来做到这一点。

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

即标注会是这样的:

{exp:runcron} 
{/exp:runcron} 

该代码会调用该插件,并且该插件将运行PHP代码,做你脑子里的任何任务。

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. <[email protected]> 
    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维基文章提供了一些洞察脚本是如何设置和安排:

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

将这个文件在你的EE“系统” 文件夹,确保可执行位 设置(使用chmod + X ee_cli_sql.php),然后 呼叫手动或通过cron。

如果你不想使用CLI(命令行界面)来管理你的cron作业,你可能会考虑从EllisLab的Cron Plugin,它可以设置为调用一个常规,计划的基础上一个插件或模块。

+0

自动关闭过期的条目链接无法使用 – williamcarswell 2013-01-28 13:21:44

+3

当EllisLab 2012年11月重新设计了自己的网站,他们似乎并没有在维基携带 - 所有的链接都是简单的301重定向到'ellislab.com'。幸运的是,我能够从[Internet Archive WayBack Machine]中找到原始文章(http://web.archive。组织/网络/ 20120103211342/HTTP://expressionengine.com/wiki/Automatically_close_expired_entries) – rjb 2013-01-28 19:30:57