crontab.xml Structure
crontab.xml declares scheduled tasks that run at specified intervals.
Basic crontab.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="vendor_product_sync" instance="Vendor\Module\Cron\ProductSync" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>
Job attributes:
name— Unique job identifierinstance— PHP class implementing the jobmethod— Method to call (usually execute)
Schedule element:
<!-- Cron expression -->
<schedule>* * * * *</schedule>
<!-- Or reference config value -->
<schedule config_path="vendor_module/cron/schedule"/></schedule>
Multiple jobs:
<group id="default">
<job name="vendor_sync" instance="Vendor\Module\Cron\Sync" method="execute">
<schedule>*/5 * * * *</schedule>
</job>
<job name="vendor_cleanup" instance="Vendor\Module\Cron\Cleanup" method="execute">
<schedule>0 2 * * *</schedule>
</job>
<job name="vendor_report" instance="Vendor\Module\Cron\Report" method="execute">
<schedule>0 0 * * 0</schedule>
</job>
</group>
Cron Expression Syntax
Cron expressions define when jobs run using a 5-field format.
Format:
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sunday=0)
│ │ │ │ │
* * * * *
Common expressions:
* * * * * Every minute
*/5 * * * * Every 5 minutes
0 * * * * Every hour
0 0 * * * Every day at midnight
0 2 * * * Every day at 2 AM
0 0 * * 0 Every Sunday at midnight
0 0 1 * * First day of every month
*/15 * * * * Every 15 minutes
0 9-17 * * * Every hour from 9 AM to 5 PM
Special characters:
* Any value
, List (1,3,5)
- Range (1-5)
/ Step (*/5 = every 5)
Examples:
0 0 * * * Daily at midnight
*/10 * * * * Every 10 minutes
0 */2 * * * Every 2 hours
0 9 * * 1-5 Weekdays at 9 AM
0 0 1,15 * * 1st and 15th of month
Config-based schedule:
<job name="vendor_sync" instance="Vendor\Module\Cron\Sync" method="execute">
<schedule config_path="vendor_module/cron/schedule"/>
</job>
// In config.xml
<default>
<vendor_module>
<cron>
<schedule>*/10 * * * *</schedule>
</cron>
</vendor_module>
</default>
Cron Job Implementation
Cron job classes implement the scheduled task logic.
Basic cron job:
<?php
namespace Vendor\Module\Cron;
use Magento\Framework\Logger\Monolog\Logger;
class ProductSync
{
public function __construct(
private Logger $logger,
private \Vendor\Module\Service\SyncService $syncService
) {}
public function execute(): void
{
$this->logger->info('Product sync cron started');
try {
$this->syncService->syncProducts();
$this->logger->info('Product sync completed');
} catch (\Exception $e) {
$this->logger->error('Product sync failed: ' . $e->getMessage());
}
}
}
Cron with return status:
public function execute()
{
try {
$this->processItems();
return true; // Success
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return false; // Failure
}
}
Running cron manually:
# Run all cron jobs
php bin/magento cron:run
# Run specific group
php bin/magento cron:run --group default
# Check cron status
php bin/magento cron:status
Cron logging:
-- Check cron job history
SELECT * FROM cron_schedule ORDER BY executed_at DESC LIMIT 10;
-- Find pending jobs
SELECT * FROM cron_schedule WHERE status = 'pending';
-- Find failed jobs
SELECT * FROM cron_schedule WHERE status = 'error';
Cron Groups
Cron groups allow organizing jobs and controlling execution frequency.
Custom cron group:
<!-- etc/crontab.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<!-- Default group -->
<group id="default">
<job name="vendor_cleanup" instance="Vendor\Module\Cron\Cleanup" method="execute">
<schedule>0 2 * * *</schedule>
</job>
</group>
<!-- Custom group -->
<group id="vendor_import">
<job name="vendor_import_products" instance="Vendor\Module\Cron\ImportProducts" method="execute">
<schedule>*/15 * * * *</schedule>
</job>
<job name="vendor_import_orders" instance="Vendor\Module\Cron\ImportOrders" method="execute">
<schedule>*/30 * * * *</schedule>
</job>
</group>
</config>
Cron group configuration:
<!-- etc/cron_groups.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
<group id="vendor_import">
<schedule_generate_every>15</schedule_generate_every>
<schedule_ahead_for>20</schedule_ahead_for>
<schedule_lifetime>15</schedule_lifetime>
<history_cleanup_every>10</history_cleanup_every>
<history_success_lifetime>60</history_success_lifetime>
<history_failure_lifetime>480</history_failure_lifetime>
<use_group>
<group>default</group>
</use_group>
</group>
</config>
Group attributes:
schedule_generate_every— Minutes between schedule generationschedule_ahead_for— How far ahead to generate schedulesschedule_lifetime— Minutes a schedule item stays validhistory_cleanup_every— Minutes between history cleanuphistory_success_lifetime— Minutes to keep success recordshistory_failure_lifetime— Minutes to keep failure records
Running specific group:
php bin/magento cron:run --group vendor_import
Quiz
1. What cron expression runs a job every 5 minutes?
2. What method is called by the cron runner?
3. What is the default cron group name?
4. Where is cron schedule history stored?
Flashcards
Question
What XML file declares cron jobs?
Click to reveal answer
Answer
crontab.xml
Question
What does * * * * * mean?
Click to reveal answer
Answer
Run every minute
Question
What is the standard cron job method name?
Click to reveal answer
Answer
execute()
Question
How do you run cron manually?
Click to reveal answer
Answer
php bin/magento cron:run
Question
What table stores cron execution history?
Click to reveal answer
Answer
cron_schedule
Revision Notes
Key Takeaways
- 1. crontab.xml declares scheduled jobs with cron expressions
- 2. Cron expressions use 5-field format: minute, hour, day, month, weekday
- 3. Cron groups organize jobs and control execution settings
- 4. Job classes implement execute() method
- 5. cron_schedule table tracks execution history
- 6. Run cron via php bin/magento cron:run
Interview Tips
- • Explain cron expression syntax with examples
- • Describe how to create custom cron groups
- • Know how to debug cron job failures
- • Discuss cron scheduling and history cleanup
Cheat Sheet
crontab.xml Cheat Sheet
Structure:
<group id="default">
<job name="job_name" instance="Class" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
Cron expressions:
- = Every minute
- */5 * * * * = Every 5 minutes
- 0 * * * * = Every hour
- 0 2 * * * = Daily at 2 AM
- 0 0 * * 0 = Weekly on Sunday
CLI:
- cron:run — Run all jobs
- cron:run --group name — Run specific group
- cron:status — Check status
Table: cron_schedule