How to configure Magento cron job
Cron job is an important component for correct Magento performance. It is widely used for running actions that are performed on schedule, such as indexing and caching, email dispatches, sitemap generation, currency rates updates, and many more actions as well.
Today we would like to explain how to configure Magento cron job correctly. The configuration process can vary a bit depending on your site’s control panel (cPanel, Plesk, etc).
- How often should you run cron executions for Magento?
- Commands to run cron in Magento
- Adding a new Magento cron task via SSH
- Adding a new Magento cron task via cPanel
- Check Magento cron status
- Common mistakes of Magento cron configuration
How often should you run cron executions for Magento?
Our recommendation is to run cron every 5 minutes. We have seen various recommendations, from once in an hour to once a minute, but once in every 5 minutes from our experience is a right choice for a typical middle-sized Magento shop. If you set up cron job for running every 5 minutes and use SSH, the part of the command which is responsible for the schedule will look like this:
*/5 * * * *
Commands to run cron in Magento
The commands used to run cron are different for various Magento versions.
php /path/to/your/magento/site/root/bin/magento -- --quiet cron:run
Do not forget to change /path/to/your/magento/site/root to the path of your website’s default folder. Magento 1.x has the option to run cron via your site URL, but we don’t recommend doing this. Such method causes issues with running several simultaneous cron processes, which can also be followed with server performance troubles and task completion failures.
Adding a new Magento cron task via SSH
Log in to the server via SSH. Run crontab -e command to add a cron task. At this point you will see a text editor, where you can add or edit cron tasks. Starting from a new line, add the following record:
*/5 * * * * sh /path/to/your/magento/site/root/cron.sh
Again, don’t forget to insert your own default folder path!
Save the changes and close the file. If you did everything correctly, crontab -l command will show you the newly created task.
Adding a new Magento cron task via cPanel
To set up a new cron task in Magento via cPanel, log into your site’s configuration panel at
http://yourmagentosite.com:2082/ or https://yourmagentosite.com:2083/.
Then, enter cron into the search field, and you’ll see a Cron jobs option in the Advanced section.
To set up a cron job with this method, you may need to enter an email address in the Cron email section. This email will be used to send cron messages to, which is useful in case of any cron job execution issues.
Now let’s set the cron schedule and the command to be run.
Press Add New Cron Job to finish your configuration.
Check Magento cron status
To make sure that your cron task works correctly, you can go and see over the changes that appear in the cron_schedule table of the Magento database. If its contents are changing, then you configured cron in the right away.
NB: Do not manually change anything in this database table! At this point everything you should do is watch.
Here’s the example. We opened the table:
Common mistakes of Magento cron configuration
Making sure the instructions are valid
From our experience, the most common mistakes while setting cron on any website are caused by copying instructions from the Internet. Very often those instructions are correct but need adaptation for your server or were correct at the moment when the article was written but aren’t correct any more.
To make sure you are using the correct instuctions, refer to your hosting company support articles or ask your system administrator.
For example, a random article says that the cron job command looks like this:
*/5 * * * * /var/www/magento/cron.sh
This is correct, but you also should remember, that permissions for /var/www/magento/cron.sh file should allow its execution. Here’s a universal method where you let the command interpreter to execute the file:
*/5 * * * * sh /var/www/magento/cron.sh
Here’s another example of a command taken from a random article:
*/5 * * * * /usr/bin/php /var/www/magento/cron.php
What if it doesn’t work for you? Maybe in your case PHP is installed into another folder (such as /usr/local/bin/php or /opt/php/bin/php), so it’s better to use cron.sh file, which searches for the PHP interpreter itself.
Using email address for cron
Make sure you use the correct email address in MAILTO. If there’s an issue with cron task execution, you will receive an email with the error description, which in case of troubles will help you to understand what went wrong.
Here’s how to set cron for Magento and add an email address for receiving messages:
MAILTO="email@example.com" */5 * * * * sh /path/to/magento/cron.sh
MAILTO="firstname.lastname@example.org" */5 * * * * php /path/to/magento/bin/magento -- --quiet cron:run
As always, don’t forget to replace the example address with your own email and insert the correct path to your Magento default folder.
Output redirection issues
Another common mistake you should know about is output redirection to /dev/null. It is used if some warnings are reported during the cron task executions, and the owner doesn’t want to correct the issues. Here’s how the command looks like:
*/5 * * * * sh /var/www/magento/cron.sh >/dev/null 2>&1
In that case any output from cron task is suppressed to avoid receiving constant warning messages. The thing is, if somehow some fatal errors are caused during cron execution, you won’t know about them, too, and it can slow down the investigating and getting rid of the troubles.
The same effect can be seen if you set an empty MAILTO.
We hope that this article was helpful for you. If you still feel like you don’t fully understand what to do, don’t hesitate turning to a specialist.