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

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

Google Ads Conversion Tracking Through Magento Platform:

Spread the love Google Ads Conversion Tracking Through

Object Pool Pattern in Magento2

Spread the love What is Object Pool Pattern in Magento 2 An

JENKINS

Spread the love JENKINS Introduction and Installation

Virtual Type and Type

Spread the love Virtual type is one of the most common

Customize the length of Input field

Spread the love Sometimes we need to change the limit the

How to Add Custom Tab on Admin Dashboard and Count Website Total Visitor in Magento 2?

Spread the love In this blog we’ll see How to Add Custom

How to Add Sign Out Tab in Customer Account in Magento 2?

Spread the love In this blog we’ll see How to Add

How to create custom tab in product detail page in magento 2?

Spread the love In this blog we’ll see how to add

Understanding and Implementing CORS & CSRF

Spread the love What is security? The world has become more

bluethinkinc Blogs