开发者

Cron jobs with Expression Engine Scripts

开发者 https://www.devze.com 2023-02-13 18:49 出处:网络
Just wondering if anyone has any advice on the best approach to take for setting up cron jobs for scripts that run within Expression Engine.

Just wondering if anyone has any advice on the best approach to take for setting up cron jobs for scripts that run within Expression Engine.

At the moment my plan is to use a cron job to access a URL with Lynx. The URL will be a random string so it can't be stumbled upon but will be publicly accessible. The cron job will load the URL, and the script will run as part of expression engine.

It seems the开发者_开发百科 ideal way to run these scripts though would be to get a cron job to run a PHP script internally, but at this time I need it to run things from within the EE framework so calling my module script would fail as it wouldn't be piped in.

How could I pipe this in to work or should I just go with plan A?


On the EE2 Forums and the EE1 Forums, you'll find many people using cron jobs for various things.

The most popular use seems to be automatically closing expired entries, using a command line PHP script with a scheduled cron job:

#!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.
?>

The ExpressionEngine Wiki article provides some insight into how the script is setup and scheduled:

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.

If you'd rather not use the CLI (command line interface) to manage your cron job, you might consider the Cron Plugin from EllisLab, which can be setup to call a Plugin or Module on a regular, scheduled basis.


I would think the easiest option would be just to have the PHP script separate that does this.

However, to accomplish this in EE, you would need to create a plugin or extension(I can never remember which is supposed to do what tasks) that executes whatever PHP code you want to run and then do a extension or plugin call out from whatever template page you have created to access via a URL.

i.e. the callout would be something like:

{exp:runcron}
{/exp:runcron}

That code would call the plugin, and the plugin would run the PHP code that does whatever task you had in mind.

0

精彩评论

暂无评论...
验证码 换一张
取 消