Configure and run cron

Overview of cron

Several Magento features require at least one cron job, which schedules activities to occur in the future. A partial list of these activities follows:

  • Catalog price rules
  • Newsletters
  • Generating Google sitemaps
  • Customer Alerts/Notifications (product price change, product back in stock)
  • Reindexing
  • Private sales (Magento Commerce only)
  • Automatic updating of currency rates
  • All Magento e-mails (including order confirmation and transactional)

We recommend you run cron as the Magento file system owner. Do not run cron as root; we also recommend against running cron as the web server user.

You can no longer run dev/tools/cron.sh because the script has been removed.

Magento depends on proper cron job configuration for many important system functions, including indexing. Failure to set it up properly means Magento won’t function as expected.

UNIX systems schedule tasks to be performed by particular users using a crontab, which is a file that contains instructions to the cron daemon that tell the daemon in effect to “run this command at this time on this date”. Each user has its own crontab, and commands in any given crontab are executed as the user who owns it.

To run cron in a web browser, see Secure cron.php to run in a browser

Run cron from the command line

Command options:

bin/magento cron:run [--group="<cron group name>"]

Where --group specifies the cron group to run. Omit this option to run cron for all groups.

To set up custom cron jobs and groups, see Configure custom cron jobs and cron groups.

You must run cron twice: the first time to discover tasks to run and the second time to run the tasks themselves. The second cron run must occur on or after the scheduled_at time for every task.

Run cron in the background

This section discusses how to run all Magento cron jobs every minute, which is the recommended interval for both Magento Open Source and Magento Commerce.

Run Magento cron jobs as the Magento file system owner.

Prerequisites

Magento uses cron for two sets of tasks, and for each, cron can run with a different configuration:

  • PHP command-line configuration: The general cron job that reindexes indexers, generates e-mails, generates the sitemap, and so on.

    You can find the command-line configuration using the command php --ini.

  • Web server PHP plug-in configuration: Two other cron jobs are used by the Component Manager and System Upgrade utilities.

    You can find the web server plug-in configuration using phpinfo.php.

  • To avoid issues during installation and upgrade, we strongly recommend you apply the same PHP settings to both the PHP command-line configuration and to the PHP web server plug-in’s configuration. For more information, see Required PHP settings.
  • In a multi-node system, crontab can run on only one node.

    This applies to you only if you set up more than one webnode for reasons related to performance or scalability.

Find the PHP binary and php.ini path

To display the path to your PHP binary, enter

which php

A sample result follows:

/usr/bin/php

Create the cron job

To create a cron job for the Magento file system owner, enter the following command as a user with root privileges:

crontab -u <Magento file system owner user name> -e

For example,

crontab -u magento_user -e

A text editor displays. (You might need to choose a text editor first.)

* * * * * <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

where

  • <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
  • | grep -v "Ran jobs by schedule" filters this message from the log, making any errors easier to spot

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.

If you're a contributing developer (that is, you cloned the Magento 2 GitHub repository), only the first line applies to you. See the examples that follow for details.

Example 1: Everyone except contributing developers

For example, if the PHP binary is located in /usr/bin, you installed Magento in /var/www/magento2, enter

Example:

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

Example 2: Contributing developers only (that is, you cloned the Magento 2 GitHub repository):

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

The preceding works for contributing developers because if you cloned the GitHub repository you don’t have an update directory and errors result if you run magento setup:cron:run. These cron jobs work in the Web Setup Wizard, which contributing developers cannot use for installing or updating the Magento application or components.

Save your changes to the crontab and exit the editor.