Magento Cron

Magento handles cron through a single cron.php file

Running this is documented by Magento.

  • crontab -e to create or edit the the crontab, the user creating the crontab is correct.

  • */5 * * * * /bin/sh /[your Magento install dir]/cron.sh to run cron every 5 minutes, debug crontab here.

Magento cron gotchas

  • Magento does prevent duplicate runs, but only silently. This means it won't sanity check a task that's been running for an inordinately long time
  • It's not able to be paralled, so legitimately long-running tasks will hold up any other tasks

Using Edmonds Commerce's Cron Runner with Magento

The EC Cron Runner could be used to run tasks outside of Magento.

To do this, remove from the module's config.xml the cron task in question. Keep a note of the <model>module/observer::methodName</model> line.

Then create a file resembling an example within Magento's shell/ folder. Something like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
require_once('/path/to/magento/app/Mage.php');
Mage::app('admin');
require_once '/path/to/magento/shell/abstract.php';
class Mage_Shell_YourTask extends Mage_Shell_Abstract
{
    /**
    * Run script
    *
    */
    public function run()
    {
        // Call the method here that was previously configured in the module's cron configuration
        $model = Mage::getSingleton('module/observer');
        $model->methodToCall();
    }
}
$shell = new Mage_Shell_YourTask();
$shell->run();

Once this is in place, add it as a new task as documented in the cron runner docs.