Magento 2 is a complex unity of features that help you to build a successful, easily-manageable store. There are a lot of processes to be handled in Magento 2 admin that require automation and scheduling. And that is exactly where cron job comes in handy. So...

Let's find out what exactly is it, why you need it and how to create custom cron job in Magento 2.

What is Magento cron job?

Magento cron job — is one of the most important Magento 2 features. It helps to configure commands or script that systematically runs and performs the tasks you intend it to.

With the cron job you don't need to manually reindex, generate google sitemaps, send Magento emails, update currency rates etc. The cron job does it all for you automatically, on a specific date and time if you configure it correctly. 

Why use cron job in Magento 2?

As mentioned before, cron job simplifies your Magento 2 management by performing certain tasks by schedule. With cron job you don't need to do a lot of monotonous work manually or stay up late waiting for the most convenient time to do the task. The cron job has you covered. 

These are the most common cron job activities:

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

However, that is far from all. Many Magento 2 extensions use cron job to regularly update the data. And our Magento 2 extensions are not an exception. For example:

1. In Magento 2 Dynamic Category that allows you to create automatic categories has 2 cron jobs:

  • 1st updates dynamic product attributes
  • 2nd applies dynamic category rules

2. In Magento 2 WebP Images extension the cron runs and converts images to modern WebP format.

3. In Magento 2 Mautic Integration extenison the cron asynchronously runs the API requests to Mautic.

Note: before creating your custom cron job, make you have the crons by opening the crontab as the Magento file system owner. Run this command:

crontab -l

The result should be as follows: 

#~ 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

Follow these steps to create a 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
    }
}

3. Add the etc/crontab.xml file (configurable cron job file) there:

<?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 your cron job will be included in. We recommend adding it to the default group;
vendorname_modulename_cron_somecronmodel - the name of your cron job. It must be unique, so we recommend creating it from the name of the cron job model;
VendorName\ModuleName\Cron\SomeCronModel - the name of the cron job model;
execute - the name of the executed method in the cron job model;
* * * * * - the frequency of the cron job. Learn more about this format on Wikipedia.

Also, visit the official documentation for more information on cron jobs and cron groups in Magento 2.