Experience Super Fast Managed Hosting | Upto 2 Months FREE | Use Coupon Code: MGT25

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.

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:

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:

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:

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!

About Author

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.