How to Create Custom Cron Job in Magento 2

When we're developing new functionality in Magento 2 we need to automate some processes based on cron. So, in this article, we are going to guide you on how to create custom cron in Magento 2.

Before we start I assume, you have already created a custom module. If you don't have it or don't know how to create it then check out our other article How To Create a Magento 2 Module.

Let's get started !!

Step 1: Create crontab.xml at app/code/Devhooks/HelloWorld/etc/ and paste the below code. You need to change code as per your requirement.

saveCopyzoom_out_map
<?xml version="1.0" ?>
<config>
    <group id="default">
        <job name="devhooks_custom_cron" instance="Devhooks\HelloWorld\Cron\Custom" method="execute">
            <schedule>*/5 * * * *</schedule>
        </job>
    </group>
</config>
  • group id Name of the cron group. The group name doesn’t have to be unique. You can run cron for one group at a time.
  • job name Unique ID for this cron job.
  • instance Class to be instantiated (classpath).
  • method Method in classpath to call.
  • schedule time Schedule in cron format. You can set it as per your need. Check the below graph to know more about each *.
saveCopyzoom_out_map
* * * * *
| | | | |
| | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month  (range: 1-31)
| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)

In above code, you can see we've used Devhooks\HelloWorld\Cron\Custom class for add some logic based on our requirement. So, now let's create that class.

Step 2: Create Custom.php class file at app/code/Devhooks/HelloWorld/Cron/ location and paste the below code.

saveCopyzoom_out_map
<?php

namespace Devhooks\HelloWorld\Cron;

class Custom
{
    protected$logger;

    /**
    * Constructor
    *
    * @param \Psr\Log\LoggerInterface $logger
    */
    public function __construct(\Psr\Log\LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function execute()
    {   
        $this->logger->info("Devhooks custom cron is executed.");
        //add your cron job logic here.
    }
}

In above code, we've added a logger to print simple info log. So, can add logic as per your requirements.

Bingo!!

All set, now open Command line in folder root of magento and run the below commands.

saveCopyzoom_out_map
php bin/magento s:up;
php bin/magento c:c;

If you want to run cron now then run below command.

saveCopyzoom_out_map
php bin/magento cron:run;

To check whether the Cron is working properly or not, go to var/log/cron.log and find devhooks_custom_cron you can see the text Cron Job devhooks_custom_cron is run in that file.

That’s it,

Below is the list of commands which are useful to perform cron related tasks.

Install cron

saveCopyzoom_out_map
php bin/magento cron:install

Check cron is installed or not.

saveCopyzoom_out_map
crontab -l

Run cron with all group

saveCopyzoom_out_map
php bin/magento cron:run

Run cron with specific group. You can replace your group with default.

saveCopyzoom_out_map
php bin/magento cron:run --group default

Remove cron

saveCopyzoom_out_map
php bin/magento cron:remove

We hope this Magento post helped you to know How to Create Custom Cron in Magento 2. Bookmark it for your future reference. Do comment below if you have any other questions or doubts.

P.S. Do share this post with your team.

Review other articles maybe it'll help you too.




Search
Recent Articles
Tags
Newsletter
Copyright © 2022 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off