How to Setup & Run Magento Cron Job For Your Store
Updated on December 24, 2021 8 Min Read
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
- The Magento Cron Job Use Cases
- How to Set Up Magento Cron Job in Magento 2
- Configure Magento 2 Cron Groups For Custom Module
- Run Cron In The Background For Magento 2 Cron Configuration
- How to Set Up Magento Cron Job For Version 1.x
- How to Set Up Cron Job on a Cloudways Managed Server
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 * * * * php -c <ini-file-path> <your Magento install dir>/bin/magento cron:run
- */1 * * * * php -c <ini-file-path> <your Magento install dir>/update/cron.php
- */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.
- */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&
- */1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/update/cron.php > /var/www/magento2/var/log/update.cron.log&
- */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:
- <group id=”<group_name>”>
- <job name=”<job_name>” instance=”<classpath>” method=”<method>”>
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.
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:
- <?xml version=”1.0″?>
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:
- <?xml version=”1.0″?>
- <cron_expr>*/5 * * * *</cron_expr>
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:
- 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. 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
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.
Go to the Cron Job Management section and click the Add New Cron Job button.
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.
If you want to use the command line, you can switch to the advanced editor under the Advanced tab.
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 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
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.