Categories: How ToMagento 2

How to Set Up Cron Jobs in Magento 2

Cron jobs in Magento used generally to serve the purpose of automating system administration and maintenance. It helps users to schedule jobs to run periodically based on date and time we have setup. Alike earlier Magento 1.x versions, Magento 2 requires the cron job setup to periodically run and operate some of the features like sending newsletters, creating Google, auto updation of currency, re-indexing, customer notifications and many more.

We are presenting the code to be implemented for successful cron job setup. Here, we have set the cron job to run every 5 minutes.

/usr/bin/php -c /[full path of magento root]/bin/magento cron::run >/dev/null 2>&1
/usr/bin/php -c /[full path of magento root]/update/cron.php >/dev/null 2>&1
/usr/bin/php -c /[full path of magento root]/bin/magento setup:cron:run >/dev/null 2>&1

Same as above, learn to setup cron job for Magento 1.x. If you have any comments or suggestions, please feel free to let us know.

Click to rate this post!
[Total: 16 Average: 2.6]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • I am not having access to my SSH.
    I want to reindex my indexers. Is it possible to reindex the indexers using cpanel by setting the cron jobs?
    If yes, then please let me know how to setup? What are the commands for cron?

    • If you have only concern about re-indexing then you can do it by programmatically using the magento 2 code.

      or ask hosting guys for help with cron.
      For Magento 2 SSH access is required and Recommanded. So Choose hosting accordingly.

  • what line of code should i apply all of them or 1 line of code

    /usr/bin/php -c /[full path of magento root]/bin/magento cron::run >/dev/null 2>&1
    /usr/bin/php -c /[full path of magento root]/update/cron.php >/dev/null 2>&1
    /usr/bin/php -c /[full path of magento root]/bin/magento setup:cron:run >/dev/null 2>&1

  • Should i add all the lines

    /usr/bin/php -c /[full path of magento root]/bin/magento cron::run >/dev/null 2>&1
    /usr/bin/php -c /[full path of magento root]/update/cron.php >/dev/null 2>&1
    /usr/bin/php -c /[full path of magento root]/bin/magento setup:cron:run >/dev/null 2>&1

    or only 1 line

    /usr/bin/php -c /[full path of magento root]/bin/magento cron::run >/dev/null 2>&1

  • hello,
    thank you very much for this. I have Magento 2 installed, I want to add the Cron Job. Do I do this from cPanel? Do I add exactly the same as your tutorial in Magento 1? Also my cron.php file is in "pub" folder, is this OK?
    thanks for your help

    • Yes David, you can setup it from cPanel. You can try the same as tutorial and if it does not work, you will need only to change the path of php in command. Replace the path with the one where your php is installed. There is no issue with your cron.php under pub folder, it will work any way.

  • Thanks for your response earlier. I followed this post and replaced [The path to Magento root ] with my url. I was trying to instal extensions after magento installation but it kep complaining of cron jobs.

    Kindly assist.

    Thanks

    • Hello Mathew,

      Make sure the path is correct, if you can't figure out, please contact us. We will be happy to help you with it.

  • Getting the following errors:

    PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20131226/mcrypt.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20131226/mcrypt.so: cannot open shared object file: No such file or directory in Unknown on line 0

    [InvalidArgumentException]
    There are no commands defined in the "cron" namespace.
    Did you mean this?
    setup:cron

    PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20131226/mcrypt.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20131226/mcrypt.so: cannot open shared object file: No such file or directory in Unknown on line 0

    [InvalidArgumentException]
    The PDO extension is required for this adapter but the extension is not loaded

    [Zend_Db_Adapter_Exception]
    The PDO extension is required for this adapter but the extension is not loaded

    PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20131226/mcrypt.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20131226/mcrypt.so: cannot open shared object file: No such file or directory in Unknown on line 0

    • can you please contact us with the issue, we will help you with it or guide you in proper way.

      • For this cron job 'php -c /etc/php5/cli/php.ini /home/a5newstore3/public_html/update/cron.php' I get the following error:

        X-Powered-By: PHP/5.6.21
        Content-type: text/html; charset=UTF-8

        Cron readiness check failed

        For this cron job 'php -c /etc/php5/cli/php.ini /home/a5newstore3/public_html/bin/magento cron:run' I get the following error:

        X-Powered-By: PHP/5.6.21
        Content-type: text/html; charset=UTF-8

        bin/magento must be run as a CLI application

        For this cron job 'php -c /etc/php5/cli/php.ini /home/a5newstore3/public_html/bin/magento setup:cron:run' I get the following error:

        X-Powered-By: PHP/5.6.21
        Content-type: text/html; charset=UTF-8

        bin/magento must be run as a CLI application

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago