Magento 2 Cron

Magento 2 Cron tutorial on how to to configure and what is Cron needed for.

How Cron works #

Magento 2 Cron uses the hosting’s server Cron for some of it’s functionality. This is needed for processing essential parts of the application:

  • All Magento e-mails like Order Confirmation, Transactional Emails Newsletter sending - Magento 2 does not send these emails at once but queues them and sends in batches. If your store is not sending any order confirmation emails Cron running properly is a first point to check.

  • Catalog price rules updates.

  • Customer Alerts and Notifications - if a Customer chooses to be notified when a Product comes back in stock, or there is a price change for example, on the Product Page.

  • Generating a Sitemap - as you update the Product Catalog, for example add a new Product, the sitemap.xml can be left in a state where it’s missing it, this is where cron processes a new Sitemap and adds the new Product.

  • Reindexing - changes to Products or Categories can be left behind as the index has old data stored.

  • Updates the Currency Rates

Cron Setup #

To setup the cron tasks in your system you can run:

bin/magento cron:install

The cron setup command can return either:

Crontab has been generated and saved

or if you already have the cron tasks installed:

Crontab has already been generated and saved

Cron Sample #

Crontab can be checked in your System via command line:

crontab -l

This will output something in the form below:

#~ MAGENTO START
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run | grep -v Ran jobs by schedule >> /var/www/html/magento2/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/html/magento2/update/cron.php >> /var/www/html/magento2/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento setup:cron:run >> /var/www/html/magento2/var/log/setup.cron.log
#~ MAGENTO END

Make sure /var/www/html/magento2/ above matches the path of your Magento Root folder.

Cron Run #

To trigger a Magento 2 Cron run you can execute the command line:

php bin/magento cron:run

The output of cron run after execution should be:

Ran jobs by schedule.

Cron Test #

So you’ve setup Cron using the method above but you are not sure if it is really working ? Below are ways to test if Cron is running in your Magento 2 store.

Cron log #

SSH / FTP into your hosting and in the wwwroot/var/log/ folder check if any of these files exist:

  • cron.log
  • magento.cron.log
  • update.cron.log
  • setup.cron.log

Cron Table #

You can also check the cron tasks that ran or are scheduled in your Database by running following query:

select * from cron_schedule

The list returned will show when each task ran, when it finished (if it did):

- Job ID and code
- Status
- Created date
- Scheduled date
- Executed date
- Finished date 

Cron Disable / Remove #

To disabled or remove a Magento 2 Cron you can execute the command line:

php bin/magento cron:remove

The output of the removal command after execution should be:

Magento cron tasks have been removed

Need help with Magento 2 Cron ?