How to Setup & Run Magento Cron Job For Your Store

Updated on December 24, 2021  8 Min Read

magento cron

Managing any ecommerce store is a serious challenge. There is always a long list of things to do and it seems that almost every task is urgent.

Thankfully, Magento powered store owners can turn to Magento cron job, an incredibly useful utility that allows stores to set up, configure, and execute commands and tasks at a specified date and time.

For both store owners and developers, the Magento cron job is an essential component of the platform that executes scripts at predefined intervals. As such, cron jobs are an indispensable part of every web application. However, in order to best utilize the benefit of cron jobs, it is important to understand when to create Magento cron jobs.Table Of Content: Magento Cron Job

  1. The Magento Cron Job Use Cases
  2. How to Set Up Magento Cron Job in Magento 2
    1. Before Magento 2 Cron Configuration
    2. Creating Magento 2 Cron Job
  3. Configure Magento 2 Cron Groups For Custom Module
    1. Run Cron Jobs On The Command Line
  4. Run Cron In The Background For Magento 2 Cron Configuration
    1. Requirements For Running Cron in Background
    2. Web Server Configuration
    3. PHP command-line
  5. How to Set Up Magento Cron Job For Version 1.x
  6. How to Set Up Cron Job on a Cloudways Managed Server
  7. Conclusion

The Magento Cron Job Use Cases

For a typical Magento store, cron jobs take care of a host of mundane activities that take up a lot of administrator’s time. From sending newsletter emails to performing pre-scheduled tasks such as indexing and caching, sitemap generation, currency rates updates. In fact, store administrators can use Magento cron jobs for automating almost all tasks that can take up time that could be better improve the overall operations of the store.

In this article, I will try to cover major aspects of a Magento cron job, so make sure you read till the end.

How to Set Up Magento Cron Job in Magento 2

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 time. Magento executes 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 module could run the Magento cron file. If you don’t have a server to try out setting up a cron job, signup for a free trial account on the Cloudways Platform and avail the opportunity right now.

Managed Magento Hosting for an Instant Performance Boost

Add & schedule cron jobs with absolute ease on the fastest cloud hosting platform.TRY CLOUDWAYS FOR FREE

Before Magento 2 Cron Configuration

Before configuring Magento 2 cron, you need to carry out certain steps to make sure the process of setting up and executing cron jobs goes smoothly.

Log in to the Magento 2 server as a master user (who has permissions to write to the Magento 2 file system). If you use the Bash shell, you can use the following command to switch to the Magento file system owner:

su <Magento 2 file system owner> -s /bin/bash -c <command>

Creating Magento 2 Cron Job

To create a Magento 2 cron job, you must log in as a user with root privileges (as mentioned earlier) and run following command:

crontab -u <Magento 2 file system owner user name> -e

For example, if the username is magento_cloudways, the command would be

crontab -u magento_cloudways -e

Now since the following commands are long and there is a chance of a mistake, I recommend using a text editor.

  1. */1 * * * * php -c <ini-file-path> <your Magento install dir>/bin/magento cron:run
  2. */1 * * * * php -c <ini-file-path> <your Magento install dir>/update/cron.php
  3. */1 * * * * php -c <ini-file-path> <your Magento install dir>/bin/magento setup:cron:run

The first command reindexes the indexers, sends automated emails and generates the sitemap. This command is associated with the PHP command line .ini file.

The other two commands are used for the Component Manager and System Upgrade.

Example:

  1. */1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/bin/magento cron:run > /var/www/magento2/var/log/magento.cron.log&
  2. */1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/update/cron.php > /var/www/magento2/var/log/update.cron.log&
  3. */1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/bin/magento setup:cron:run > /var/www/magento2/var/log/setup.cron.log&

Now save all changes to the crontab.

Configure Magento 2 Cron Groups For Custom Module

If you wish to set up a cron for a custom module Magento, you have the option of either using the default or a different group.

For configuration of the cron group for your module, create the crontab.xml file in the <your magento2 component base dir>/<vendorname>/module-<name>/etc/crontab.xml with this following code:

  1. <config>
  2. <group id=”<group_name>”>
  3. <job name=”<job_name>” instance=”<classpath>” method=”<method>”>
  4. <schedule><time></schedule>
  5. </job>
  6. </group>
  7. </config>

In the above code:

  • <group_name> is the cron group name. Remember that you can only run cron for a single group at a time.
  • <job_name> is a unique ID used for identifying the cron job.
  • <classpath> is the name of the group.
  • <method> indicates the method called in the classpath.
  • <time> is scheduled in cron format.

Run Cron Jobs On The Command Line

The following command allows you to run cron on Magento 2 command line:

magento cron:run [–group=”<cron group name>”]

The –group switch defines the cron group to run. You can remove it if you wish to use the default group.

Note: You may run cron for one group at a time. You must run cron twice, the first time to discover the tasks to run, and the second time, to run the tasks themselves).

Run Cron In The Background For Magento 2 Cron Configuration

As you may know, Magento CE and EE editions have a recommended process of executing Magento 2 cron jobs.

Requirements For Running Cron in Background

Magento cron job is run with different configurations. The general cron job that reindexes, generates emails and sitemap usually runs as the PHP command-line users php.ini. Other cron jobs are used by the component manager and system upgrade utilities. These commands must be used in the web servers, php.ini

If you don’t have experience with running cron, you can use all commands with the web server’s configuration.

Web Server Configuration

To find out the web server configuration, run the phpinfo.php file in the web browser and search for the loaded configuration file option.

PHP command-line

After web server configuration, you need to do the PHP command-line configuration by the following command:

php -i | grep php.ini

As a result you get something like this:

Configuration File (php.ini) Path => /etc/php5/cli

Loaded Configuration File => /etc/php5/cli/php.ini

How to Set Up Magento Cron Job For Version 1.x

I will demonstrate how to set up a cron job for Magento 1 through a custom Magento module, Cloudways_Cron. This will require the following files:

  • $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 crontab.
  • $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:

  1. <?xml version=”1.0″?>
  2. <config>
  3. <modules>
  4. <Cloudways_Cron>
  5. <active>true</active>
  6. <codePool>local</codePool>
  7. </Cloudways_Cron>
  8. </modules>
  9. </config>

Next, 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:

  1. <?xml version=”1.0″?>
  2. <config>
  3. <modules>
  4. <Cloudways_Cron>
  5. <version>1.0</version>
  6. </Cloudways_Cron>
  7. </modules>
  8. <crontab>
  9. <jobs>
  10. <custom_cron_task>
  11. <schedule>
  12. <cron_expr>*/5 * * * *</cron_expr>
  13. </schedule>
  14. <run>
  15. <model>cron/cron::crontask</model>
  16. </run>
  17. </custom_cron_task>
  18. </jobs>
  19. </crontab>
  20. <global>
  21. <models>
  22. <cron>
  23. <class>Cloudways_Cron_Model</class>
  24. </cron>
  25. </models>
  26. </global>
  27. </config>

In the above code, I have declared the version of the module and the model class. The important part is the <crontab> element that I use 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 between the instances of cron job execution. As you can see, I have set the execution interval to 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:

  1. <?php
  2. class Cloudways_Cron_Model_Cron
  3. {
  4. public function crontask()
  5. {
  6. // send email
  7. $mail = Mage::getModel(‘core/email’)
  8. ->setToEmail(‘user@email.com’)
  9. ->setBody(‘Body of the Automated Cron Email Goes Here’)
  10. ->setSubject(‘Subject: Cron Task (every 5 minutes) ‘.date(“Y-m-d H:i:s”))
  11. ->setFromEmail(‘admin@example.com’)
  12. ->setFromName(‘Your Store Name’)
  13. ->setType(‘html’);
  14. $mail->send();
  15. }

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. While this was a simple example, you could set up complex tasks with multiple concurrent subtasks.

For instance, I can set up a cron job configuration to update currency rates for visitors coming from different countries. Here is a complete article on how to schedule a currency update task with Magento cron job

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/cron.sh

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

Optimize Magento Speed Like a Pro

Subscribe now and get a free ebook to your inbox. I agree to the Cloudways Terms of Service & Privacy PolicySUBSCRIBE NOW

How to Set Up Cron Job on a Cloudways Managed Server

Log into the Cloudways Platform with your credentials and click Applications in the top menu bar. Select the application for which you want to set up the cron job.

Magento cron job Cloudways app console

Go to the Cron Job Management section and click the Add New Cron Job button.

Magento cron job Cloudways app console

Next,  you have to configure the cron job. For that, you need to decide how often the cron job will run by selecting a value from the dropdown menu (you can enter a value of your own as well).

You can also set the type of script that you will be running (PHP, cURL, or Wget) and the command to execute. Click Submit to save your settings.

Magento cron job management

If you want to use the command line, you can switch to the advanced editor under the Advanced tab.

Magento cron job advanced

Now if you want to monitor the currently active and running cron jobs, click Monitoring in the left menu bar, click Analytics, and choose Running Crons from the given choices.

Magento cron job monitoring

Magento 2 Cron Job FAQs

Q. What is Magento 2 cron job?

Magento cron job — is one of the most important Magento 2 features. It helps to configure commands or scripts that systematically run and perform the tasks you intend it to. With the cron job you don’t need to manually reindex, generate google sitemaps, send Magento emails, update currency rates etc.

Q. How can I tell if a cron job is running Magento?

To verify whether or not your crontab is set up do the following steps: Log in to your Magento server as, or switch to, the Magento file system owner. See if the following file exists: bash ls -al /var/. setup_cronjob_status If the file exists, cron has run successfully in the past.

Q. How do I remove the cron job in Magento 2?

To remove the Magento cron job:

  • Log in as, or switch to, the Magento file system owner in the terminal
  • Change to your Magento installation directory: cd /
  • Enter the following command: php bin/magento cron:remove

Final Words

For online store owners, it is a huge time saver to not get sidetracked by tasks that could be automated. Why not focus on your core strength, selling? This is why Magento 2 cron jobs are an easy option to manage tasks on Cloudways Magento Hosting Platform. Try it by starting your free trial today. If you run into any issue, you can contact the 24/7/365 Support who will help you with any issue.

I hope you have found this article useful in setting up cron jobs. If you want me to add any custom Magento cron job, do let me know in the comment section.

https://www.cloudways.com/blog/setup-magento-cron-job/