>How to setup Cron Jobs in Magento 2

How to setup Cron Jobs in Magento 2

To create a cron job in Magento 2, firstly you need to log in to ssh then enter the following command crontab -u -e.

For example: crontab -u magento_user -e.

You can see current Cron Jobs by using crontab -l. To fix a Cron Job or add a new one you can use crontab -e, then an editor pops up allowing you to edit the cron job.

How to write a Cron Job command.

* * * * *  <path to php binary> <magento install dir>/bin/magento cron:run | grep -v "Ran jobs by schedule" >> <magento install dir>/var/log/magento.cron.log
* * * * *  <path to php binary> <magento install dir>/update/cron.php >> <magento install dir>/var/log/update.cron.log
* * * * *  <path to php binary> <magento install dir>/bin/magento setup:cron:run >> <magento install dir>/var/log/setup.cron.log

In which:

<path to php binary> is the absolute file system path to your PHP binary.

<magento install dir> is the directory in which you installed the Magento software, for example: /var/www/html/magento2/

The first command (magento cron:run) reindexes indexers, sends automated e-mails, generates the sitemap, and so on. Usually it’s associated with the PHP command line .ini file. The other two commands are used by the Component Manager and System Upgrade.

For example: you install Magento in /var/www/magento2, to set cron job running every 1 minute, you can follow as below.

*/1 * * * * php /var/www/html/magento2/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/html/magento2/var/log/magento.cron.log
*/1 * * * * php /var/www/html/magento2/update/cron.php >> /var/www/html/magento2/var/log/update.cron.log
*/1 * * * * php /var/www/html/magento2/bin/magento setup:cron:run >> /var/www/html/magento2/var/log/setup.cron.log

To save it after all modification, click Esc then use :wq and enter. In case you don’t want save any changes as above you can use :q enter.

< Previous Post
Next Post >