How to Create Cron Job in Magento 2

How to Create Cron Job in Magento 2

How to Create Cron Job in Magento 2

Hello geeks, you will find many tutorials related to the article Magento 2 create cron job or How to Create Cron Job in Magento 2. Today in this blog we will also put contributions to those blogs posted for creating a cron job in Magento 2. Cron jobs are helpful for scheduling our tasks to be run at defined time frames.

In this blog we will create two main files:

1) We have to create a file crontab.xml in the etc folder e.g. Vendor/Module/etc/crontab.xml.

crontab.xml

<?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 instance="Vendor\Module\Cron\CronTaskName" method="execute" name="cron_task_name">
            <schedule>*/15 * * * *</schedule>
        </job>
    </group>
</config>

This cron job we have made in the default group, you can change the group or can create your own custom group by simply changing the group id name in crontab.xml, like: <group id=”custom”>

We added the time to run the cron in the schedule tag. We have added */15 * * * * in the schedule tag, so this cron job will execute every 15 minutes intervals. For your reference to the cron schedule, we have elaborated the cron schedule format below.

     *              *            *             *             *

Minutes    Hours      Day       Month   Weekday

(0 - 59)    (0 - 23)  (1 - 31)   (1 - 12)    (0 - 7)

2) Now we will create the cron task execution file in the Cron folder, which we have described in the crontab.xml file. e.g. Vendor/Module/Cron/CronTaskName.php

CronTaskName.php

<?php

namespace Vendor/Module\Cron;

use Psr\Log\LoggerInterface;

class CronTaskName
{

    protected $logger;

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

    public function execute()
    {
        $this->logger->info('Custom cron job is working!');
    }
}

Now run this cron by typing and executing the following command in CLI.

But first, register this command if not done previously and run cache flush or cache clean if registered the module.

php bin/magento cron:run –group=”default”

or

php bin/magento cron:run

Check your system.log for the text “Custom cron job is working!”. If it’s found then your cron job is working fine.

To check more if the Cron Jobs working fine or not there is a Plugin of Amasty which is Cron Schedule List by Amasty which helps to track and manage all tasks running in the website background with the easy-to-use Cron Scheduler tool. This plugin also has a built-in Magento 2 Cron Scheduler to schedule tasks on a separate grid. This is a paid extension from Amasty.

Suppose you want a free extension for checking Magento 2 cron jobs running. In that case, you can go for the Magento 2 Cron Schedule from Mageplaza which is also one of the most useful Cron Scheduler plugins and helps you to execute tasks with no effort from the admin panel. This extension is available on GitHub whose link is https://github.com/mageplaza/magento-2-cron-schedule.

To disable the cron job in Magento 2 check this article https://techurbane.com/disable-cron-job-using-config-in-magento-2/.

Also, check our article on Magento 2 must-have extensions for supercharging your store https://techurbane.com/must-have-magento-2-extensions-supercharge-your-store/.

Please Like and Share if this article helped you. Cheers!

4.7 3 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments