Chat with us, powered by LiveChat

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.

Create And Setup A Cron Job In Magento Stores

March 27, 2019

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 PHP 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 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 set up cron job, signup for a free account on Cloudways platform and avail the opportunity right now.

Scalable, Super Fast, & Secure Magento Hosting

Our Clients Love us because we never compromise on these features

In this article, I will discuss the Magento cron configuration in below 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:

<?xml version="1.0"?>

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

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

<?xml version="1.0"?>
          <cron_expr>*/5 * * * *</cron_expr>

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 for Magento cron job configuration.

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:


class Cloudways_Cron_Model_Cron
  public function crontask()
    // send email
    $mail = Mage::getModel('core/email')
     ->setBody('Body of the Automated Cron Email Goes Here')
     ->setSubject('Subject: Cron Task (every 5 minutes) '.date("Y-m-d H:i:s"))
     ->setFromName('Your Store Name')

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:

*/5 * * * * sh /path/to/your/magento/store/

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

In the above function, I have setup a cron job that will send 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, running every five minutes. You could set up complex cron job configuration that execute several tasks at the same time.

Similarly, I can setup cron job configuration to update currency rates if my store is selling items in the international market. Here is a complete article on how to schedule a currency update task with Magento cron job.


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

Share This Article

Boost Your Magento Store Performance by 5x Times & Maximize Your Sales

Our fastest Magento hosting can help you in growing your business revenue by 500%

Fayyaz Khattak

Fayyaz, a passionate Motorbike tourist, works as a Team Lead — Magento Community at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. You can contact him at

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

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

BFCM 2019