How to Create Custom Cron Job in Magento 2?

How to Create Custom Cron Job in Magento 2

Magento 2 is a powerful ecommerce platform with many features to run a successful store. Being such complex software, there are so many wheels turning on schedule.

Many processes are running on automation that is handled by Cron Jobs.

In this quick tutorial, we will learn what a cron job is and how to create a custom cron job in Magento 2.

What is a Magento 2 Cron Job?

Magento 2 cron job is a common command line utility feature – that schedules a job to run periodically at fixed times or intervals. The job is defined through the script that is executed by the cron.

There are many tasks in the Magento 2 platform that work on automation. The cron job runs those tasks at a defined time. You have to configure the job, and the time it runs on.

The time triggers the cron to execute.

Why use the cron job in Magento 2?

Cron job helps you to manage Magento tasks by performing many tasks that could be challenging to do if done manually. The monotonous manual work is then written down in script and scheduled to run on time.

The cron job will do it for you – when you are not available.

Most common cron job activities:

  • Reindexing
  • Updating currency rates
  • Updating catalog price rules
  • Customer alerts/notifications
  • Generating Google sitemaps
  • Sending newsletters/all Magento emails

Ensure Cron

Before creating the custom cron job, you have to ensure that the store has a cron. Run the following command to open the crontab as the Magento file system owner:

crontab -l

You will see this result:

#~ MAGENTO START c5f9e5ed71cceaabc4d4fd9b3e827a2b
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /var/www/html/magento2/var/log/magento.cron.log
#~ MAGENTO END c5f9e5ed71cceaabc4d4fd9b3e827a2b

How to create Custom Cron Job in Magento 2?

Follow the steps to create custom Cron job in Magento 2:

  1. Create a basic module
  2. Add the Cron/SomeCronModel.php file (cron job model) to it:
<?php
namespace VendorName\ModuleName\Cron;
class SomeCronModel
{
    public function execute()
    {
         //your cron job code
    }
}
  1. Add the etc/crontab.xml file (configurable cron job file):
<?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="vendorname_modulename_cron_somecronmodel" instance="VendorName\ModuleName\Cron\SomeCronModel" method="execute">
            <schedule>* * * * *</schedule>
        </job>
     </group>
</config>

default: the ID of the group you want to launch the cron job. We suggest using defau;lt group.

vendorname_modulename_cron_somecronmodel: Name of the cron job. Keep it unique so no two cron conflicts.

VendorName\ModuleName\Cron\SomeCronModel: The name of the cron job model.

execute: Define the execution method for the cron job.

* * * * *: Frequency of the cron job.

Configure Cron in Magento 2

Once the cron is created, you have to configure the cron options from the Magento 2 admin dashboard. 

Follow the steps to configure the cron options to make it run as expected:

Step 1: Navigate to Stores > Configuration > Advanced > System > Cron (Scheduled Tasks)

Create Custom Cron Job in Magento 2

You will find all of the default cron jobs under the Cron (Scheduled Tasks) tab along with the cron jobs that go with other Magento 2 modules you use. Each cron has the same Options, so you only have to fill the preferred value.

Step 2: Configure cron schedule options

Create Custom Cron Job in Magento 2

Generate Schedules Every: How often do you want the cron to be scheduled? 1 means every minute.

Schedule Ahead for: How far in advance to schedule the cron job? If you put 15 here, the cron job will be scheduled for the next 15 minutes.

Missed if Not Run Within: Give the cron ‘missed’ status after the time it failed to run. In minutes.

History Cleanup Every: History of the completed cron will be cleared after this minutes.

Success History Lifetime:  Duration (in minutes) during which the cron jobs with the Successful status should remain in the database.

Failure History Lifetime: Duration (in minutes) during which the cron jobs with the Failed status should remain in the database.

Use Separate Process: Run the cron of each group separately.

Once all the fields are filled, click on the ‘Save Config’ to save the configuration.

Create and Configure Cron Job in Magento 2 with Breeze

If you are using Breeze platform to manage your Magento store, you don’t have to go through this entire process.

Simply login to your account and set the Cron Job from the dashboard.

Follow the steps to add cron jobs:

  1. Login to your Breeze Platform
  2. Select the Company you would like to work on
  3. Click on the ‘Sites’ to open site list
  4. Open the ‘Store’ from the list
  5. Click on the ‘Cron’ from the option at Left
Create Custom Cron Job in Magento 2 in Breeze
  1. Click the ‘Add Cron’
  2. Fill the pop-up box and create a new Cron job

You can add any number of Cron jobs from here. If you want to edit or delete the Cron job, you can do it from here.

Wrap Up

Creating a custom Cron job might be challenging if you are not a developer and well-versed in Magento. We made it easier with the Breeze platform – because many store owners asked for it.

If you are not on Breeze yet, feel free to get in touch and I will give you a demo and show you how easier it is to create custom Cron Job in Magento 2.

Latest Magento Tips, Guides, & News

Stay updated with new stuff in the Magento ecosystem including exclusive deals, how-to articles, new plugins, and more. 100% Magento Goodness, a promise!

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top

Talk to a sales representative

USA / Worldwide

+1.714.2425683

India

+91.9852704704

We can help you. Right now.

Fast growing merchants depend Breeze for high-performance hosting. Experience counts. Let's get started.

Request demo

Please fill in the details below and we’ll reach out to you with a customized demo of our product!

Get your FREE Image SEO Checklist

Bring more visitors to your image from Google search from the existing images.