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.

How to debug Mangeto 1 Cron

  • Check that the Magento cron is present in crontab -l
  • Check the magento default logs in public/var/log
  • Check the cron task log (assuming there is one) public/var/log
  • You can sort the log files using ls -haltr
  • Use magerun sys:cron:list to list all the scheduled corn jobs
  • Use magerun sys:cron:history to see what jobs ran/are running
  • You can use ps aux | grep cron to see if the cron process is running
  • In case you need to terminate the process use kill <process_pid>
  • As a last resort, the process can be killed using kill -9 <process_pid>
  • Cron jobs can be run manually using magerun sys:cron:run <cron_job_name>