How To Set and Configure Custom Cron Jobs In Magento 2?

Blog
Spread the love

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

Step 1.2 : Create VendorName\ModuleName\registration.php

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. 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.

Step 4: Run the cron job

We can run the Magento cron jobs using the below command.

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:

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:

Run the command at least twice.

Step 6.2 : Clean the Magento cache:

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

bluethinkinc_blog

2022-08-12

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

Implementation of WireMock for better testing

Spread the love WireMock is a flexible and powerful tool

Magento 2: How to selected default option in configurable options?

Spread the love Configurable products in Magento 2 allow

How To Implement Simple JWT Authentication in Django Rest Framework

Spread the love We’ll learn how to use simple JWT

Optimizing Search: Integrating Elasticsearch in Django Applications

Spread the love What is Elasticsearch? Elasticsearch is a

How to create Customer Segments

Spread the love In Magento 2, “customer

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

bluethinkinc Blogs