This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Create And Setup A Cron Job In Magento Stores

Updated on  15th May

4 Min Read
Reading Time: 4 minutes

The best thing about Magento is the long (and impressive) list of features that add a lot of value for all users. Of these, Magento Cron Job is an incredibly useful utility that allows stores to setup and configure execution of commands and tasks on a specified date and time.

Magento Cron Job is an essential component of the platform that is ideal for executing scripts at predefined intervals. Given this capability, cron forms part of every web applications. However, a more important question is when to create Magento cron jobs.

Magento Cron Schedule

Typically, there are a lot of opportunities for adding Magento cron jobs. From sending newsletter emails to performing scheduled tasks such as indexing and caching, sitemap generation, currency rates updates. In fact, store administrators use Magento cron jobs for almost all automated tasks that improve the overall performance of the store.

By default, Magento cron is integrated into the platform’s core. You can add, and then schedule tasks in the Magento module for execution on regular predefined intervals. Magento runs all the cron tasks using the cron.php and cron.sh files, located at the root of the Magento store. All you need to do is to make sure that your server level cron could run Magento cron file. If you don’t have a server to run Magento cron job, signup for a free account on Cloudways platform and avail the opportunity right now.

In this article, I will discuss the configuration of a Magento cron job in a module. I will create a custom Magento module Cloudways_Cron. Following is the list of required files for the task:

  • $Magento_Root/app/etc/modules/Cloudways_Cron.xml file enables the module.
  • $Magento_Root/app/code/local/Cloudways/Cron/etc/config.xml is a module configuration file that contains the cron tab.
  • $Magento_Root/app/code/local/Cloudways/Cron/Model/Cron.php is the model file in which I will set the logic of the scheduled task.

The first step is to enable the custom module in the Magento store by creating an XML file in $Magento_Root/app/etc/modules/. The name of the XML file will be the combination of namespace and module name.

Start  with creating $Magento_Root/app/etc/modules/Cloudways_Cron.xml with the following code:

Next, I will create a module configuration file in which I will define the crontab with specific interval.

Create $Magento_Root/app/code/local/Cloudways/Cron/etc/config.xml with the following code:

In the above code, I have declared the version of our module and the model class. The important part is the <crontab> element. I use it to configure the cron job.

Next is the <jobs> element which contains the <custom_cron_task> child element. This is the unique identifier for the cron job. As you can see, I have only scheduled a single task, but you can set up multiple cron jobs inside the  <jobs> element.

The <cron_expr> element under the <schedule> element defines the interval on which the job will run regularly. In the above case, the cron job will run every five minutes. Now, you might be wondering what it will do every five minutes?

That is mentioned in the <run> element! I have defined a function crontask from the Cloudways_Cron_Model model class which will be called by Magento store once the cron job is executed.

Finally, I created a model file $Magento_Root/app/code/local/Cloudways/Cron/Model/Cron.php with the following code piece:

In the above crontask function, I sent an email using the Magento core/email model. This email will be sent every five minutes when the cron job is executed. It will be called at every cron job run on every five minutes. You could set up complex cron jobs that execute several tasks at the same time.

Last but not the least, I will create a cron job entry into my server. I will simply add the following line to my crontab file:

Remember, do not forget to replace /path/to/your/magento/store with the actual path of your Magento installation.

Conclusion

I hope that by now you know how to setup a custom cron job in Magento store. Setting up a custom Magento module and then configuring Magento cron jobs is pretty straightforward.

Share your opinion in the comment section. COMMENT NOW

Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.