Magento 2 Cron Configuration

- Magento 2

How to configure Cron in Magento 2

In the following article, we shed light on proper Magento 2 cron configuration. Below, you will find general advices about configuring and running cron, section related to component manager and system upgrade with cron, and a short troubleshooting guide.

Note that there are a lot of Magento 2 extensions that utilize cron by default. There is no need to configure cron separately, since everything is available in the appropriate section of extension’s settings. For instance, you can check our Improve Import, as it fully leverages the Magento 2 cron functionality. Thus, you can create a schedule for various import/export processes synchronizing your store with third-party platforms.

How to configure and run cron in Magento 2

Correct Magento 2 cron configuration is extremely important because various Magento features are based on scheduled activities possible only via cron. So, let’s list some of them:

  • Newsletters
  • Catalog price rules
  • Generating Google sitemaps
  • Customer Notifications
  • Private sales
  • Reindexing
  • Automatic currency rate update
  • Magento emails

Since Magento 2 depends on appropriate cron job configuration for vital system functions, failure to run it suitably leads to the inability of the whole Magento system to work as expected.

It is also necessary to mention that in UNIX systems scheduled tasks are based on a crontab – a file with instructions to a cron daemon. These instructions tell the daemon when appropriate commands should be performed.

Magento 2 Cron Configuration: First steps

Before setting up Magento 2 cron configuration, it is necessary to perform some important prerequisites.

  1. Running cron as a Magento file system owner  is a vital condition (you can find more information here). Please note that it not possible to run dev/tools/cron.sh in Magento 2, because this script is no longer useful within the system.
  2. For running various commands from any Magento directory, don’t forget to enhance your system PATH with <your Magento install dir>/bin. Take into consideration that each shell has its own syntax, so it is necessary to examine such sources as stackexchange before going further. For instance the CentOS bash shell looks as follows:

There are also other ways to run commands:

in case of

run them as

Alternatively, you can rely on

where <your Magento install dir> is a web server’s docroot subdirectory.

Magento 2 Cron Configuration: Running cron in the background

The optimal interval for running cron is one minute. Below, we discuss how to  start Magento cron jobs every 60 seconds.

Prerequisites

Since Magento relies on cron for two groups of tasks, there is a different configuration for each one. The first set of tasks is based on the general cron job. It includes reindexing, generating emails and the sitemap, as well as other similar tasks. Such processes are based on the user’s command line php.ini.

As for the second group of tasks, it is related to Component Manager and System Upgrade utilities and based on web server’s php.ini. Cron for Component Manager and System Upgrade is described further in this post.

Please note that you can run all commands with the aid of web server’s configuration.

Web server configuration

Now, let’s find out your web server configuration:

  • Run phpinfo.php in the browser
  • Find the Loaded Configuration File option

PHP command-line configuration

The second step requires displaying the PHP command-line configuration. To achieve this goal, utilize the below command:

As a result, you should get something similar to this example:

Creating the cron job

Log in as a user who has root privileges and run the below command:

Utilize the following commands while working with a text editor:

Please note that <ini-file-path> indicates the path to your php.ini file used for the cron job.

The magento cron:run command is related to the first set of actions described above. As for the next two commands, they are utilized by the Component Manager and System Upgrade utilities.

Don’t forget to save changes before closing the editor.

Magento 2 Cron Configuration: Configuring and running cron via the command line

You can start cron in Magento 2 with the help of the command line. Additionally, it is also possible to configure a cron group for every custom module.

Magento 2 cron groups and custom modules

A cron group enables you to run cron for several process simultaneously. Here, Magento modules are divided into two groups: the default group and the index group. To configure a module cron group:

create <your component base dir>/<vendorname>/module-<name>/etc/crontab.xml;

Fill it with the following content:

where:

  • <group_name> is a name of your cron group. Please note that it is not necessary to use a unique name here. You can only run cron for a single group at a time
  • <job_name> is a unique ID used to identify the cron job.
  • <classpath> – the name speaks for itself.
  • <method> is highly connected to the above option, since it indicates a method to call in the classpath.
  • <time> is a schedule. Use cron format only and omit this parameter if your schedule is defined somewhere else, for instance in the Magento database.

As for the <group> element, it supports the next options:

  • schedule_generate_every is used to determine the frequency schedules are noted in your cron_schedule table (in minutes).
  • schedule_ahead_for is used to set the same parameter in advance.
  • schedule_lifetime shows the lifetime of a schedule in minutes.
  • history_cleanup_every is used to set time for cron history to be stored in the database.
  • history_success_lifetime indicates the number of minutes a record related to a successfully completed cron job is stored in the database.
  • history_failure_lifetime – the same parameter aimed at failed cron jobs.
  • use_separate_process: 1 indicates that all cron jobs should run in parallel; in case of 0 they run one after the other.

Running сron from command line

The following command option enables you to run cron in Magento 2 via the command line:

Please note that –group specifies a particular cron group. You should omit the option in case of the default group.

And don’t forget to run cron twice: to discover tasks and to run them.

Magento 2 Cron Configuration: Component Manager and System Upgrade

The following section of the post is dedicated to Component Manager and System Upgrade. To enable the updater working with both of them, two cron jobs are required; each one should run at an interval of one minute. For the Magento file system owner Enable them as crontabs.

Now, as a user with root privileges, check if you’ve already set up a crontab.

On CentOS, ruu the following command:

If there is no crontab for a particular user, you will get the following message:

Hence, to set up crontabs, be logged as a root privileges user and run the below command:

In the text editor, enter the aforementioned commands related to Component Manager and System Upgrade:

Don’t forget to save the changes before closing the editor.

Magento 2 Cron Troubleshooting

In case of failed cron readiness check, you should find out if someone has already set up a cron job. If not, the Component Manager will display the following messages:

Each one indicates the appropriate cron job.

To verify if a crontab is set up in your Magento 2, be logged as a user with root privilege into the server and enter this command:

In case the crontab has not been set up, you will see the following message:

If your Magento file system owner lacks a cron job, configure it as described above.

If cron runs with errors, try to start commands manually, because each one might display error messages helpful in solving the problem.

Key commands are described above, but you should also run the updater. In <your Magento install dir>/update, run:

If you see the following error or a similar message, composer install is not running, consequently it does not update dependencies related to the updater application:

Useful Links

This Magento 2 Cron guide is based on the following official documentation. You can find more information about the Magento 2 cron configuration by following the links below: