Cron jobs are used to schedule commands to be executed periodically by the cron. Cron is a a time-based job scheduler software in Unix-like computer operating systems. It is driven by a crontab
(cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule.
Magento cron jobs is a different kind of functionality than that just mentioned. It merely relies on the system cron software to trigger the root Magento cron.php
or cron.sh
files periodically. Keep that in mind while talking about cron and Magento cron jobs.
It is highly important that the system administrator has the system cron set to trigger the Magento cron.sh
file at regular intervals, at least every five minutes. This can be done by adding the following line to system cron:*/5 * * * * /path/to /magento/root/folder/cron.sh
. This way, you as a Magento extension developer have the ability to create a new Magento cron jobs definitions through your extensions configuration files and rest assured they will get executed. Magento cron jobs are defined in the config.xml
file as follows:
<?xml version="1.0"?> <config> <!-- … other elements ... --> <crontab> <jobs> <foggyline_happyhour_ordersToFulfilment> <schedule> <cron_expr>*/2 * * * *</cron_expr> </schedule> <run> <model>foggyline_happyhour/service::ordersToFulfilment</model> </run> </foggyline_happyhour_ordersToFulfilment> </jobs> </crontab> <!-- … other elements ... --> </config>
In this example, our cron job has been defined with the name foggyline_happyhour_ordersToFulfilment
and set to execute every two minutes, which can be seen by the schedule >cron_expr
value. For more details on writing cron expressions, check out the following URL: http://en.wikipedia.org/wiki/Cron.
The model
element, which in this case is set to foggyline_happyhour/service::ordersToFulfilment
, means that the following code from the app/code/community/Foggyline/HappyHour/Model/Service.php
file would get executed by this cron job:
<?php class Foggyline_HappyHour_Model_Service { public function ping() { Mage::log('ping'); } }
The same as with observer classes, cron-defined model classes do not need to extend anything.
Even though this Magento cron job has been set to run every two minutes, you as an extension developer have no guarantee that the system cron will ever be run on a third-party website, or if it's run it might not be run in small enough intervals. Thus, you might end up with a code logic that never gets executed, or gets executed in larger than planned intervals.
During development, however, you do not need to have the system cron set up. It is sufficient to just execute the http://magento.loc/cron.php
URL in the browser, as this will trigger the Magento cron system.
For example, if you were writing an extension that has one or more Magento cron jobs defined in config.xml
, the easiest way to check if you correctly defined your cron job would be to truncate the cron_schedule
database table and then trigger the http://magento.loc/cron.php
URL in the browser. Obviously, in this case, later on you would have to periodically trigger the http://magento.loc/cron.php
URL to check if your cron job execution went through, monitoring the executed_at
column within the cron_schedule
database table.