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

Create or remove the Magento crontab

This section discusses how to create or remove your Magento crontab (that is, the configuration for Magento cron jobs).

About the Magento crontab

The Magento crontab is the configuration used to run Magento cron jobs.

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.

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

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

Create the Magento crontab

Starting with version 2.2, Magento creates a crontab for you. We add the Magento crontab to any configured crontab for the Magento file system owner. In other words, if you already set up crontabs for other extensions or applications, we add the Magento crontab to it.

The Magento crontab is inside #~ MAGENTO START and #~ MAGENTO END comments in your crontab.

To create the Magento crontab:

  1. Log in as, or switch to, the Magento file system owner.
  2. Change to your Magento installation directory.
  3. Enter the following command:

    php bin/magento cron:install [--force]
    

Use --force to rewrite an existing Magento crontab.

  • magento cron:install does not rewrite an existing crontab inside #~ MAGENTO START and #~ MAGENTO END comments in your crontab.
  • magento cron:install --force has no effect on any cron jobs outside the Magento comments.

To view the crontab, enter the following command as the Magento file system owner:

crontab -l

A sample follows:

#~ MAGENTO START
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run 2>&1 | 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

Remove the Magento crontab

You should remove the Magento crontab only before uninstalling the Magento application.

To remove the Magento crontab:

  1. Log in as or switch to the Magento file system owner.
  2. Change to the Magento installation directory.
  3. Enter the following command:

    php bin/magento cron:remove
    

This command has no effect on cron jobs outside the #~ MAGENTO START and #~ MAGENTO END comments in your crontab.

Run cron from the command line

Command options:

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.

Logging

System logs provide detailed information for debugging. The following attributes apply to Magento cron logs:

  • All exceptions from cron jobs are logged by \Magento\Cron\Observer\ProcessCronQueueObserver::execute.

  • Failed jobs with ERROR and MISSED statuses are logged to the <your Magento install dir>/var/log directory.

  • Jobs with an ERROR status are always logged as CRITICAL in <your Magento install dir>/var/log/exception.log.

  • Jobs with a MISSED status are logged as INFO in the <your Magento install dir>/var/log directory (developer mode only).

All cron data is also written to the cron_schedule table in the Magento database. The table provides a history of cron jobs, including:

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

To see records in the table, log in to the Magento database on the command line and enter SELECT * from cron_schedule;.