Adding a Cron Job to Magento via config.xml: Step-by-Step Guide
Magento provides a robust cron system for recurring tasks like order cancellations, customer cleanup, and inventory management. However, configuring these crons can sometimes prove challenging. Let’s walk through the process of adding a new cron job using the config.xml file.
Understanding Magento Cron Structure
Firstly, it’s crucial to understand how Magento’s cron system is structured:
- Cron configuration: Found in
app/etc/config.xml - Scheduled jobs: Defined under
<crontab><jobs></jobs></crontab>in the above config file - Cron scheduler: Located at
/shell/shell.php -- crontab=update(can be run manually or scheduled via your server’s cron daemon)
Adding a New Cron Job
Now, let’s add a new cron job for an observer cancelOrder:
<crontab>
<jobs>
<kiwi_ordercanceller>
<schedule>
<cron_expr>*/5 * * * *</cron_expr>
</schedule>
<run>
<model>ordercanceller/observer::cancelOrder</model>
</run>
</kiwi_ordercanceller>
</jobs>
</crontab>
Here’s a breakdown of the code:
kiwi_ordercanceller: The cron job name, which should match the XML path in your observer*/5 * * * *: This is the cron expression. It means “run every 5 minutes”ordercanceller/observer::cancelOrder: The full model path to your observer function that contains the code for order cancellation
Ensuring Your Observer Function Works
Your observer should look something like this:
class Kiwi_OrderCanceller_Model_Observer {
public function cancelOrder() {
// ... your order cancellation logic here ...
}
}
Make sure:
- The file is located at
app/code/local/Kiwi/OrderCanceller/Model/Observer.php - The function name matches the one referenced in the cron configuration
- The
cancelOrderfunction contains the actual business logic for order cancellations
Why Your Cron Job Might Not Be Working
If your cron job isn’t working even after the above steps, consider these troubleshooting tips:
-
Clear Magento cache: Navigate to System > Cache Management and select all checkboxes, then click “Flush Magento Cache” or “Refresh”
-
Check cron.php status: Look for the file
cron.phpin your root folder. If it’s present, remove it or change its permissions to 755 -
Verify cron_schedule table: Check if the entry for your job appears in
core_cron_schedule. If not, you may need to manually run/shell/shell.php -- crontab=updateto refresh the schedule -
Check logs and exceptions: Look for any errors or exceptions thrown in your observer function that might be preventing the cron from running
Conclusion
Adding a new cron job via config.xml is a common task in Magento development. By following this guide, you should now be able to configure your own custom crons with ease.
Remember, Magento’s cron system runs every 15 minutes by default (unless you’ve changed the configuration). So if you need more frequent execution, use a smaller interval like */5 or */2 in your cron expression.
For further reference on Magento cron jobs, check our in-depth guide to Magento crons on LaravelCompany.
Happy coding! Let us know if you need any more help with this.