Categories: Magento 2 Tutorials

How to Setup Magento 2 Cron Job

Magento 2 is loaded with many awesome features, and Cron Job is one of the most useful among them. Cron is a time-based scheduler and it is useful to run commands automatically with scheduled time and date in Magento 2.

How to Configure Magento 2 Cron Job

  1. Create crontab.xml
  2. Create Run.php
  3. Run Magento 2 Cron Job

Why Configure Magento 2 Cron Job?

Cron is one the most important part of Magento 2. You can schedule multiple activities via cron which are listed below:
  • Generating Google sitemaps
  • Newsletters
  • Reindexing
  • Catalog price rules
  • Customer Alerts and Notifications.
  • Private sales (Magento Enterprise Edition only)
  • All Magento e-mails which also includes order confirmation and transactional.
  • Auto update currency rates
For more details, you may check the official documentation on Cron Job.

How to Configure Magento 2 Cron Job?

Today, I am going to guide you how to setup Cron Job in Magento using a custom module. I hope that you already have created a  custom module in Magento 2. So, let’s start!

Create crontab.xml

Create crontab.xml file in app/code/Magenticians/Mymodule/etc and paste the following code in it:
<?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="cron_name" instance="Magenticians\Mymodule\Cron\Run" method="execute">
<schedule>*/1 * * * *</schedule>-->
</job>
</group>
</config>
In the code above, I have scheduled it for one minute. I have set the group id default and job name as cron_name, and you can change them if you want.

Create Run.php

First, create directory Cron in your custom module so, the directory will now look like this: app/code/Magenticians/Mymodule/Cron. Now create Run.php file in app/code/Magenticians/Mymodule/Cron and paste the following code in it:
<?php
namespace Magenticians\Mymodule\Cron;

class Run 
{
   protected $_logger;
   public function __construct(
       \Psr\Log\LoggerInterface $logger
   ) 
    {
     $this->_logger = $logger;
    }

   public function execute()
   {
       //Edit it according to your requirement
       $this->_logger->debug('Cron run successfully');
       return $this;
   }
}

Run Magento 2 Cron Job

Connect your store with an SSH and go to the root directory of your Magento 2. Now run these commands:
php bin/magento cache:flush
php bin/magento cron:run
You can also run Cron job for one group only by running this command: php bin/magento cron:run –group=”your_group_name” To check whether the Cron is working properly, go to var/log/debug.log of your store and you will see the text Cron run successfully in it. Go to cron_schedule table of database and you will see a new entry in it with your Cron job name.

Final Words

Cron job is very important for Magento 2 store through which you can save your time by scheduling and automating its activities. After following this guide, I hope you are now capable to easily setup Cron Job in Magento 2. If you still have any issues or want to discuss something related to Magento 2 Cron Job, feel free to use the comment box below!
Syed Muneeb Ul Hasan

Syed Muneeb Ul Hasan is an expert in PHP and Magento, he prefers to educate users in implementing and learning Magento. When not working, he loves to watch cricket.

View Comments

    • You just have to enter your server credentials in your SSH terminal to connect it with your store. You can also Putty for it.

      • my server is localhost...and even i don't know the credentials and also SSH terminal??? Can you explain it more...I use Putty but it showing the Connection refused error????

  • Dear Syed,

    I have created a custom module and the above cron code is added accroding to you guide but I get the following error when I try to run the the run command

    [MagentoFrameworkExceptionLocalizedException]
    Invalid Document
    Element 'job': Character content other than whitespace is not allowed because the content type
    is 'element-only'.
    Line: 4

  • Hi Syed ..I have created one cron ..in custom module.. inside cron php file. i have written code to get and save data from customer and product tables..while running the cron it is showing "ran by cron schedule". but it is not executed and result message not stored in the logger file

Share
Published by
Syed Muneeb Ul Hasan

Recent Posts

Building Employee Trust and Dedication – A Complete Guide

In the highly competitive modern workplace, trust, and employee loyalty are crucial factors for long-term… Read More

3 months ago

12 Winning Strategies for Small Businesses Marketing

In the ever-evolving world of small business developing and implementing effective marketing strategies is critical to… Read More

3 months ago

Top 10 App Development Companies in Dubai 2024

With one of the highest internet penetration rates, the UAE has set out to revolutionize… Read More

3 months ago

Transforming Industries: How Amazons GPT44X is Revolutionizing AI Technology

Artificial Intelligence (AI) has been continually evolving, leading to remarkable advancements in various industries. Among… Read More

8 months ago

Top Magento 2 Extensions for 2023

Extensions, extensions and lots of extensions. We all love extensions, don’t we? After all, extensions… Read More

11 months ago

Unleashing the Power of Software Testing: Cooperating with a Testing Firm

Software quality is crucial to a firm's success across industries in the quickly changing digital… Read More

11 months ago