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 Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago