Magento uses Cron Jobs to run scheduled tasks, reindexing, generating emails, newsletters, sitemaps and much more. In Magento 2, crons can be configured easily and listed in the database table to process our tasks in scheduled time.
In order to create a custom cron job, please follow the below steps.
Step 1: Create a module
Step 1.1 : Create VendorName\ModuleName\etc\module.xml
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="VendorName_ModuleName" > </module> </config> |
Step 1.2 : Create VendorName\ModuleName\registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'VendorName_ModuleName', __DIR__ ); |
Step 2: Create crontab.xml
crontab.xml sets a schedule to run your custom cron code which is defined in the CronTest.php file.create crontab.xml under app/code/VendorName/ModuleName/etc/
1 2 3 4 5 6 7 8 |
<?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 instance="VendorName\ModuleName\Cron\Test" method="execute" name="vendorname_crontest_cron"> <schedule>* * * * *</schedule> </job> </group> </config> |
- 1. group id is your cron group name. You can run only cron for a single group at a time.
- 2. job instance is a class to be instantiated (classpath).
- 3. job method is the method in job instance to call.
- 4. job name is Unique ID for this cron job.
- 5. the schedule is the schedule in cron format.
“ * “ Meaning from Left to Right :
- 1 . First given for Minute (0 – 59).
- 2. Second given for Hour (0 – 23).
- 3. Third given for Day of month (1 – 31).
- 4. Fourth given for Month (1 – 12).
- 5. Fifth given for Day of week (0 – 7) (Sunday=0 or 7).
Step 3: Create a class to run cron
Step 3.1 : Create a directory for the class
app/code/VendorName/ModuleName/Cron/
Step 3.2: Create a Test.php file in cron folder with the following contents.
This file contains the custom cron code to be executed while the cron runs in the Magento.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace VendorName\ModuleName\Cron; use Psr\Log\LoggerInterface; class Test { private $logger; public function __construct( LoggerInterface $logger ) { $this->logger = $logger; } public function execute() { $this->logger->info('The Cron is Working'); } } |
Step 4: Run the cron job
We can run the Magento cron jobs using the below command.
1 |
php bin/magento cron:run --group="default" |
Step 5: Create custom cron group
To set up a custom cron group:
Step 5.1 : Open crontab.xml in a text editor.
Step 5.2 : Change group id =”test_cron”
Step 5.3 : Save the changes.
Step 5.4 : Create app/code/VendorName/ModuleName/etc/cron_groups.xml with the following contents:
1 2 3 4 5 6 7 8 9 10 11 |
<?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="test_cron"> <schedule_generate_every>1</schedule_generate_every> <schedule_ahead_for>4</schedule_ahead_for> <schedule_lifetime>2</schedule_lifetime> <history_cleanup_every>10</history_cleanup_every> <history_success_lifetime>60</history_success_lifetime> <history_failure_lifetime>600</history_failure_lifetime> </group> </config> |
Option | Description |
---|---|
schedule_generate_every |
Frequency (in minutes) that schedules are written to the cron_schedule table. |
schedule_ahead_for |
Time (in minutes) in advance that schedules are written to the cron_schedule table. |
schedule_lifetime |
Window of time (in minutes) that a cron job must start or the cron job is considered missed (“too late” to run). |
history_cleanup_every |
Time (in minutes) that cron history is kept in the database. |
history_success_lifetime |
Time (in minutes) that the record of successfully completed cron jobs is kept in the database. |
history_failure_lifetime |
Time (in minutes) that the record of failed cron jobs is kept in the database. |
use_separate_process |
Run this cron group’s jobs in a separate php process |
Step 6 : Run the custom cron group
Step 6.1 : Run Magento cron jobs for your custom group:
1 |
php bin/magento cron:run --group="test_cron" |
Run the command at least twice.
Step 6.2 : Clean the Magento cache:
1 |
php bin/magento cache:clean |
To verify, log in to the Magento Admin as an administrator.
Click Stores > Configuration > Advanced > System.
In the right pane, expand Cron. In your “Cron configuration options for group: test_cron” it scheduled with your configurations in cron_groups.xml.
bluethinkinc_blog
2022-08-12