Cron jobs are essential in Magento 2 for automating repetitive tasks such as reindexing, sending emails, generating reports, and cleaning logs. Sometimes, you may need to create a custom cron job for your own module to perform scheduled operations. In this guide, we’ll walk through the steps to set up a custom cron in Magento 2.
Step 1: Create cron.xml
Inside your custom module, create a etc/cron.xml file. This file defines the cron job and its schedule.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/cron.xsd">
<group id="default">
<job name="vendor_module_custom_cron"
instance="Vendor\Module\Cron\Custom"
method="execute">
<schedule>*/5 * * * *</schedule>
</job>
</group>
</config>
Here, the cron job runs every 5 minutes (*/5 * * * *).
Step 2: Create Cron Class
Next, create the PHP class that will be executed by the cron job.
<?php
namespace Vendor\Module\Cron;
class Custom
{
public function execute()
{
// Your custom logic here
// Example: log a message
file_put_contents(BP . '/var/log/custom_cron.log', "Cron executed at " . date('Y-m-d H:i:s') . "\n", FILE_APPEND);
}
}
This class contains the execute() method, which Magento calls when the cron job runs.
Step 3: Enable and Test Cron
Ensure Magento’s cron is properly set up on your server. Run:
bin/magento cron:run
Check var/log/custom_cron.log to confirm your cron job executed successfully.
Step 4: Configure System Cron
Magento relies on the server’s cron daemon. Add the following to your server’s crontab:
* * * * * php /path/to/magento/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /path/to/magento/var/log/magento.cron.log
* * * * * php /path/to/magento/update/cron.php >> /path/to/magento/var/log/update.cron.log
* * * * * php /path/to/magento/bin/magento setup:cron:run >> /path/to/magento/var/log/setup.cron.log
This ensures Magento’s cron system runs continuously.
Advanced Usage
- Groups → You can define jobs under different groups (e.g.,
index,default). - Schedules → Use cron expressions to control frequency (e.g.,
0 0 * * *for midnight daily). - Dependency Injection → Inject services into your cron class for database operations, API calls, etc.
Troubleshooting
- Check
var/log/magento.cron.logfor errors. - Ensure your server’s cron daemon is running (
service cron status). - Verify file permissions for
var/logandbin/magento. - Run
bin/magento setup:upgradeafter adding new cron jobs.
Best Practices
- Keep cron jobs lightweight; avoid long‑running tasks.
- Use queues or background processes for heavy operations.
- Log outputs for debugging and monitoring.
- Test cron jobs in development before deploying to production.
Conclusion
Setting up a custom cron job in Magento 2 allows you to automate repetitive tasks and extend your module’s functionality. By defining cron.xml, creating a cron class, and configuring the system cron, you can ensure your jobs run reliably. Following best practices will keep your store efficient and maintainable.
Happy Coding & Automating!