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.
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:
Catalog price rules
Generating Google sitemaps
Automatic currency rate update
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.
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.
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:
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.
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:
Loaded Configuration File=>/etc/php5/cli/php.ini
Creating the cron job
Log in as a user who has root privileges and run the below command:
crontab-u<Magento file system owner user name>-e
Utilize the following commands while working with a text editor:
<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:
magento cron:run[--group="<cron group name>"]
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.
crontab-u<Magento file system owner user name>-l
On CentOS, ruu the following command:
If there is no crontab for a particular user, you will get the following message:
no crontab formagento_user
Hence, to set up crontabs, be logged as a root privileges user and run the below command:
crontab-u<Magento file system owner user name>-e
In the text editor, enter the aforementioned commands related to Component Manager and System Upgrade: