How to Set Up Cron Jobs in Magento 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

Cron jobs in Magento 2

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.

Previous Article

How to Implement ACL in Magento 2 Extensions

Next Article

How to Add Top Links in Magento 2

Write a Comment
  1. 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

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

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

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

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

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

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

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

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

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨