How to configure Magento cron job

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?

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.

Magento 1.x
sh /path/to/your/magento/site/root/cron.sh
Magento 2.x
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.

Find cron settings in cPanel

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.

Setup cron email in cPanel for Magento

Now let’s set the cron schedule and the command to be run.

Add cron job in Magento via cPanel

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:

Check Magento cron
After waiting a bit, we see that the task was performed:
Checking Magento cron

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:

Magento 1.x:
MAILTO="webmaster@example.com"

*/5 * * * * sh /path/to/magento/cron.sh
Magento 2.x
MAILTO="webmaster@example.com"

*/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.

Andrey Tataranovich

Andrey Tataranovich

Andrei believes that only at work people can fully express themselves, so he does his best to implement all his knowledge while in the office. Having made computer science his hobby, Andrei is constantly developing his professional skills. But despite such a busy schedule he always finds time to read science fiction books and communicate with new interesting people.

You may also like...

24 Responses

  1. Tanja says:

    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.

    • Ksenia Dobreva Ksenia Dobreva says:

      Hey Tanja,

      thanks for asking.

      Could you please show the crontab line you’ve set up? We’ll have a look.

      • Tanja says:

        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 says:

        My crontab line was as follows: */5 * * * * /bin/sh /home/mystashco/public_html/cron.sh

        • Ksenia Dobreva Ksenia Dobreva says:

          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.

  2. Edward says:

    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

    • Ksenia Dobreva Ksenia Dobreva says:

      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.

  3. Edward says:

    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

    • Ksenia Dobreva Ksenia Dobreva says:

      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

  4. Edward says:

    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?

  5. Edward says:

    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

  6. francesco says:

    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

  7. Mico says:

    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?

    • Ksenia Dobreva Ksenia Dobreva says:

      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 says:

        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 Ksenia Dobreva says:

          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 says:

            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!

  8. Mico says:

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

  9. boby says:

    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?

  10. avish says:

    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.

    • anton says:

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *