Architect Magento | Tech Blogger | Magento Trainer
Mohamed Abbas | Architect Magento | Tech Blogger | Magento Trainer
Technical Lead | Magento Architect
If you’ve worked with Magento 2 (M2), you’re likely aware that various tasks are scheduled to run automatically at set intervals. These scheduled tasks are managed through Magento’s cron functionality.
However, the way scheduled tasks operate in Magento 2 might be more complex than it initially appears. Before you can effectively set up and configure a cron job, it’s essential to understand how they function within M2. We’ll cover what a cron job is, explain the key terms and components involved, and show how they integrate with Magento’s framework.
Before diving in, it’s helpful to clarify the main cron-related terms and understand how they differ.
Cron: A background service or daemon that runs on the server, executing scheduled tasks at specific times. Think of it as a background application that reads and executes tasks listed in crontab files.
Crontab: Short for “cron table,” this is a configuration file that contains a list of scheduled tasks, each defined by a specific time schedule. The schedule format follows a “cron expression” (e.g., * * * * *). For a detailed understanding of cron expressions, see resources like Cron Guru.
Cron Job: An individual task within a crontab file that runs at a defined time interval. A single crontab file can contain multiple cron jobs, each scheduled independently.
In standard PHP applications, multiple cron jobs are generally defined within a single crontab file, with each job executing based on the schedule set by its cron expression.
For instance, here’s an example of a crontab file containing two cron jobs:
/etc/crontab
# Run a PHP script every day at 1:00 AM
0 1 * * * /usr/bin/php /var/www/html/daily_sync.php
# Run another PHP script every 20 minutes
*/20 * * * * /usr/bin/php /var/www/html/stock_notifications.php
In Magento, things operate a bit differently. Defining numerous cron jobs for various modules within a single crontab file would be challenging to manage and maintain, especially since this file exists outside the Magento project scope. Consequently, it isn’t controlled by version control, making it harder to manage across multiple environments.
Instead, Magento uses a single, standardized cron job across all environments, typically set up as a variation of the following command:
/etc/crontab
* * * * * /usr/local/bin/php /var/www/html/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /var/www/html/var/log/magento.cron.log
The command above runs the bin/magento cron:run
script every minute, as specified by the * * * * *
cron expression. Following the command, the output is directed to the /var/log/magento.cron.log
file.
This setup ensures that Magento’s cron:run
command handles the entire management and execution of scheduled tasks. Every minute, bin/magento cron:run
executes, triggering the core Magento PHP script to process scheduled jobs.
With Magento’s single cron job setup, the platform maintains an internal cron system. This system enables custom modules to leverage Magento’s scheduling functionality.
Cron jobs play a critical role within Magento’s framework, handling various tasks such as product indexing and sending customer emails to ensure smooth operations. Magento also allows developers to create custom scheduled tasks by tapping into its internal cron system.
In the same way a crontab file is defined on a Unix server, Magento allows us to define scheduled tasks—but, as is customary in Magento, this setup is achieved through an XML file.
crontab.xml
The crontab.xml
file serves to define each cron job’s specific details, including:
This file is located in the etc/crontab.xml
directory of any Magento module, and its structure generally looks like this:
etc/crontab.xml
0 3 * * *
*/30 * * * *
In the example above, the daily_sync
and check_stock
tasks match the cron jobs we reviewed earlier, but they’re now defined in Magento’s crontab.xml
format. Here, we specify the class instance responsible for each cron job and the method that should run.
In the example, as in most crontab.xml
files in Magento, the group id
is set to default
. This default
group serves as the primary grouping for organizing cron jobs.
However, you can create a custom group to organize your cron tasks more efficiently. For instance, defining <group id="foo">
places your cron jobs into a custom “foo” group. Although the default
group is often sufficient, custom groups are useful, particularly during development, as Magento allows cron jobs to be executed by specific groups with the following command:
bin/magento cron:run --group=foo
Using this approach, only cron jobs within the “foo” group will execute, allowing you to concentrate on custom tasks without triggering all other Magento cron jobs. This can save considerable time, especially when you’re deep in development and debugging.
crontab.xml
Integration with cron_schedule
Although the primary cron:run
command triggers every minute, the specific schedules in crontab.xml
files control when each task actually runs. These crontab.xml
configurations work together with the cron_schedule
MySQL table to track execution times and determine when tasks should run next.
cron_schedule
Database TableWhen a cron job is set to run based on its crontab.xml
configuration, Magento generates an entry in the cron_schedule
database table. This table logs essential details about each cron job instance, including:
job.name
in crontab.xml
default
bin/magento cron:run
instanceEach time the Magento cron process runs, it scans for pending jobs in cron_schedule
and executes them as per schedule.
cron_schedule
Tableexecuted_at
and finished_at
timestamps to identify delays or potential bottlenecks.crontab.xml
won’t run.Magento 2’s cron system is powerful for managing automated tasks. Understanding the interaction between crontab.xml
files and the cron_schedule
table enhances your ability to create, manage, and troubleshoot cron jobs effectively.
Key Takeaways:
crontab.xml
, not directly in the system cron.cron_schedule
for errors and performance issues.cron_schedule
periodically.By applying these best practices, you can maximize Magento 2’s cron capabilities for automation, performance optimization, and seamless store operation.