Categories
Magento

Magento2 Cron and Create Cron Job in Custom Module

In this tutorial, We’ll learn how to create cron in our module but before that we should know what is cron.
If you do not want to handle a task manually, then cron is a good option. The Cron Job enables for precise time and date-based automatic running. The Cron Jobs are the ideal option for recurring projects every day or every week because of their automation.

Cron Jobs are used by Magento to do scheduled operations like reindexing and creating emails, newsletters, sitemaps, among other things. Crons can be readily set up in Magento 2 and listed in the database table to complete our duties at the appointed time.

When the cron job is run, a record with the cron job’s name, custom cron, is added to the cron schedule table. Now before start we assume the following:
(i) The Commerce application is installed in /var/www/html/magento_folder
(ii) Your Commerce database username and password are both is set
(iii) You perform all actions as the file system owner

Step 1: Create a sample module
You may utilise a custom module that you already have. Verify that the test module is registered and turned on.
Before you continue, verify that the sample module is registered and enabled.
Run the following command:

bin/magento module:status Vendor_Module

Step 2: Create a class to run cron
Create a class in following path: Vendor_Name/Module_name/Cron/Test.php and paste below code.

<?php
namespace Vendor_Name\Module_name\Cron;

use Psr\Log\LoggerInterface;

class Test {
    protected $logger;

    public function __construct(LoggerInterface $logger) {
        $this->logger = $logger;
    }

   /**
    * Write to system.log
    *
    * @return void
    */
    public function execute() {
        $this->logger->info('Hello!! Magento Cron Works');
    }
}

Step 3: Create crontab.xml
Create crontab.xml as follows in the Vendor_Name/Module_name/etc directory:

<?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="custom_cronjob" instance="Vendor_Name/Module_name\Cron\Test" method="execute">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

The Vendor_Name/Module_name/Cron/Test.php class is called by the previous crontab.xml once every minute, adding a row to the cron schedule database.

Step 4: Verify the cron job

  1. Compile the code with this command:
bin/magento setup:di:compile

2. And clean the cache with this command:

bin/magento cache:clean

3. Run Commerce cron jobs:

bin/magento cron:run

I hope this blog is resolve your issue easily.
If you still have any issue feel free to ask and let me know your views to make the better. Share this solution with your other Magento buddies via social media.

Thanks for reading.

Leave a Reply

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