How to configure Magento cron job

Table of Content

How to configure Magento cron job
Posted in: Magento 2 Guides

Magento cron job is an important component for correct performance. It is widely used for running actions that are performed on schedule, such as indexing and caching, sitemap generation, currency rate updates, and many more as well. Magento default cron functionality includes AOE scheduler, but first things first.

However, today we would like to explain in detail how you can make cron setup with no issues. The configuration process can vary a bit depending on your site control panel (cPanel, Plesk, etc).

How often should you run cron executions for Magento?

Our recommendation is to run Magento cron job not manually 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 the right choice for a typical middle-sized Magento shop. If you set up a 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.

Magento 1.X

sh /path/to/your/magento/site/root/cron.sh

Run cron on Magento 2.X

[php]php /path/to/your/magento/site/root/bin/magento -- --quiet cron:run[/php]
 
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.

If Magento 2 cron is not running properly, you can check the cron.log to find errors. 

Adding a new Magento cron task via SSH (Crontab Magento method)

Log in to the server via SSH. Run the Magento 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:

[php]*/5 * * * * sh /path/to/your/magento/site/root/cron.sh[/php]
 
Again, don’t forget to insert your own default folder path!

Save the changes and close the file. If you did everything correctly, the Magento 2 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 Cron Job Magento 2 status

To make sure cron is installed correctly on Magento 2 and your cron task works as it should, 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 Magento 2 crons the right way.

NB: Do not manually change anything in this database table! At this point, everything you should do is watch.

Here’s an example. We opened the table:

After waiting a bit, we see that the task was performed:

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 anymore.

To make sure you are using the correct instructions, 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:

[php]*/5 * * * * /var/www/magento/cron.sh[/php]
 

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:

[php]*/5 * * * * sh /var/www/magento/cron.sh[/php]
 

Here’s another example of a command taken from a random article:

[php]*/5 * * * * /usr/bin/php /var/www/magento/cron.php[/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:

Magento 1.X:

  1. [php]MAILTO="[email protected]"
  2.  
  3. */5 * * * * sh /path/to/magento/cron.sh[/php]

Magento 2.X

  1. [php]MAILTO="[email protected]"
  2.  
  3. */5 * * * * php /path/to/magento/bin/magento -- --quiet cron:run[/php]

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:

[php]*/5 * * * * sh /var/www/magento/cron.sh >/dev/null 2>&1[/php]

In that case, any output from the cron task is suppressed to avoid receiving constant warning messages. The thing is, if somehow some fatal errors are caused during cron Magento execution, you won’t know about them, too, and it can slow down the investigation and get rid of the troubles.

The same effect can be seen if you set an empty MAILTO.

Magento 2 cron not running

Try flushing the cache and running cron again:

php bin/magento cache:flush
php bin/magento cron:run

Magento 2 runs specific cron jobs manually

Any manual manipulations can result in errors. To eliminate risks, you can acquire any of these free or paid extensions on the market.

For example, Amasty Cron Scheduler gathers all  Magento cron jobs of third-party modules together with native Magento ones on a special Magento 2 Cron Jobs List grid. There you can see all the related information, like cron jobs, statuses, groups, and schedules.

To run a specific job manually, navigate to System > Cron Jobs List > then click the Run Job button to start cron run and create tasks for a particular cron job:

run cron job manually

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 to turn to a specialist.

October 11, 2016
October 18, 2016
October 6, 2016
Comments
Tanja
October 18, 2016
Hi, Thank you for this post! I have followed your directions to the letter but I am getting this error when the cron is running: [18-Oct-2016 12:55:02 UTC] PHP Notice: Undefined index: SCRIPT_NAME in /home/path/public_html/cron.php on line 40 [18-Oct-2016 12:55:02 UTC] PHP Notice: Undefined index: SCRIPT_FILENAME in /home/path/public_html/cron.php on line 41. Any Help will be greatly appreciated.
Reply
Ksenia Dobreva
October 19, 2016
Hey Tanja, thanks for asking. Could you please show the crontab line you've set up? We'll have a look.
Tanja
October 19, 2016
Hi, seems that that magento notice isn't the reason my cron wasn't working it is the cron command executed in the cron.php: shell_exec(escapeshellcmd("/bin/sh $cronPath $fileName -malways 1 > /dev/null 2>&1 &")); I changed it to: shell_exec(escapeshellcmd("/bin/sh $cronPath $fileName")); Now it seems to be working. I am not sure why it was not working or if there is a better way to fix it.
Tanja
October 19, 2016
My crontab line was as follows: */5 * * * * /bin/sh /home/mystashco/public_html/cron.sh
Ksenia Dobreva
October 20, 2016
Hey Tanja, your crontab line was correct, it's hard to tell from here, but looks like something in the Magento itself prevented cron from working correctly.
Edward
October 28, 2016
Hi, I want to set up my chrons in cpanel chron and understand everything but what to put in the command line for each magento task that needs a chron. Following is a list of the ones that appear to be missing a chron. Category Products: Reindex required Product Categories: Reindex required Product Price: Reindex required Product EAV: Reindex required Stock: Reindex required Catalog Rule Product: Reindex required Catalog Product Rule: Reindex required Here is a copy of an existing chron in the list that appears to have been set up automatically somehow: php /home/domainname/public_html/store/update/cron.php >> /home/domainname/public_html/store/var/log/update.cron.log
Reply
Ksenia Dobreva
October 31, 2016
Hey Edward, thanks for your question. Looks like it's Magento 2, right? If yes, you have to set up 3 crons for it. * * * * * php /home/domainname/public_html/store/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /home/domainname/public_html/store/var/log/magento.cron.log * * * * * php /home/domainname/public_html/store/update/cron.php >> /home/domainname/public_html/store/var/log/update.cron.log * * * * * php /home/domainname/public_html/store/bin/magento setup:cron:run >> /home/domainname/public_html/store/var/log/setup.cron.log http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-cron.html#create-the-cron-job Hope that helps.
Edward
October 31, 2016
Hi Ksenia, Thank you. All three of those are already there. They must have been created by the magento installer. I used softaculous. However I still have several indexes not updating as follows. Is that not chron related? Category Products: Reindex required Product Categories: Reindex required Product Price: Reindex required Product EAV: Reindex required Stock: Reindex required Catalog Rule Product: Reindex required Catalog Product Rule: Reindex required
Reply
Ksenia Dobreva
November 1, 2016
Hey Edward, Could you please check logs to know if cron is executed at all? (var/log/*.cron.log) Also you can run the indexing manually: php bin/magento indexer:reindex
Edward
November 3, 2016
Ksenia, it took a while to get answers for this. I didn't have shell access to my shared server. The logs indicate the chrons are all running. If you have any other suggestions please let me know. Is there a reindex button in magento somewhere?
Reply
Edward
November 3, 2016
I just got another response from my server host suggesting I add this chron /opt/alt/php56/usr/bin/php /home/domainname/public_html/store/bin/magento indexer:reindex I added it for once per day and will let you know if that solves the problem
Reply
Ksenia Dobreva
November 9, 2016
How are the things going on? Did it work?
francesco
November 4, 2016
Hi, I have some problem to work cron at Magento 1.9 this is my cron comand: php /home/domainname/public_html/magento/cron.php > But not work cron
Reply
Ksenia Dobreva
November 10, 2016
Hey there, try to set up your cron like this: /bin/sh /home/domainname/public_html/magento/cron.sh Hope it helps.
Mico
January 27, 2017
Hello, Working with Magento 2 and setup cron jobs as described in above comments: * * * * * php /home/domainname/public_html/store/bin/magento cron:run | grep -v “Ran jobs by schedule” >> /home/domainname/public_html/store/var/log/magento.cron.log * * * * * php /home/domainname/public_html/store/update/cron.php >> /home/domainname/public_html/store/var/log/update.cron.log * * * * * php /home/domainname/public_html/store/bin/magento setup:cron:run >> /home/domainname/public_html/store/var/log/setup.cron.log However, I get error messages 1) "/bin/bash ......public_html/bin/magento: Permission denied" AND 2) one additional error for the one cron job with "| grep": grep: jobs: No such file or directory grep: by: No such file or directory grep: schedule”: No such file or directory What can be done?
Reply
Ksenia Dobreva
January 30, 2017
Hey Mico, thanks for asking. As for the 1st point, there's a lack of permissions. It's better to run cron from a user which owns Magento, and for Magento 2 an ideal case is the user that runs the web server. As for the second point, are you sure you didn't miss “ before Ran? In that case grep would think all the words are patterns, while “Ran jobs by schedule” is the pattern altogether. Hope that helps.
Mico
January 30, 2017
Thanks for answering Ksenia - really appreciate it. I got it, i will try these. For permissions, i am running these via cPanel on vps - that should be ok, huh?
Ksenia Dobreva
January 31, 2017
Hey Mico, I'm sorry, but it's really hard to say from here - depends on how and what you're doing precisely. Good luck!
Mico
February 1, 2017
Hey Ksenia, i managed to get 2 of those commands to work. Now only problem is the: * * * * * php /home/domainname/public_html/store/bin/magento cron:run | grep -v “Ran jobs by schedule” >> /home/domainname/public_html/store/var/log/magento.cron.log I get following message when cron tries to run: grep: jobs: No such file or directory grep: by: No such file or directory grep: schedule”: No such file or directory So it seems that for some reason it is recognizing -"Run-, but then latter part of -jobs by schedule"- is not recognized and indexer is not updating. Any idea what might be the problem with this specific command? Should this "Run jobs by schedule" text be available in some file in magento folder? Any help would be really appreciated!
mow
July 23, 2020
cron expects " as quotes and you apparently have fancy quotes which are not recognized as such. Blame your word processor for replacing " with typographically correct, but configfile-y useless “” quotes.
Mico
February 1, 2017
And by the way, problem 1 was solved by deleting 2.1.x magento and installing 2.0.x version which doesnt have any problems with permissions..
Reply
boby
February 5, 2017
Hi! I created cron module and it worked fine but I faced with a problem when I set 0 */12 * * * In config.xml it doesn’t send mail every 12 hours. Can you tell me is it a right expression or not? Can you give me the right variant for every 12 hours cron set?
Reply
Alexandra Zhos
February 6, 2017
Boby, this expression should work if you need to run your cron every 12 hours: * 0,12 * * * Try it!
avish
February 7, 2017
Hi, i create a file "google_feed.php" in magento root folder. i want to execute that file periodically. please guide me to set cron for this file.
Reply
anton
February 8, 2017
Try to create the "google_feed.php" file inside the model of your module and in the configuration file specify it and a function. I think it should work.
Guest
September 18, 2019
3
Reply
smm panel
October 22, 2021
I am so grateful for your blog post.Thanks Again. Will read on
Reply
Leave your comment

Your email address will not be published

This blog was created with Amasty Blog Pro

This blog was created with Amasty Blog Pro

© 2009-2024 Amasty. All Rights Reserved.