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:
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.
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.
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:
1
php-i|grep php.ini
As a result, you should get something similar to this example:
1
2
Configuration File(php.ini)Path=>/etc/php5/cli
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:
1
crontab-u<Magento file system owner user name>-e
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;
<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:
1
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.
1
crontab-u<Magento file system owner user name>-l
On CentOS, ruu the following command:
1
crontab-umagento_user-l
If there is no crontab for a particular user, you will get the following message:
1
no crontab formagento_user
Hence, to set up crontabs, be logged as a root privileges user and run the below command:
1
crontab-u<Magento file system owner user name>-e
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:
1
2
3
4
5
6
Error from Setup Application Cron Script:
Cron Job has notbeen configured yet
PHP Version,PHP Settings andPHP Extensions Check will fail because they depend on thischeck
Error from Updater Application Cron Script:
Cron Job has notbeen configured yet
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:
1
crontab-u<Magento file system owner name>-l
In case the crontab has not been set up, you will see the following message:
1
no crontab formagento_user
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:
1
php cron.php
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:
1
2
PHP Warning:require_once(/var/www/html/magento2/update/vendor/autoload.php):failed toopen stream:No such file ordirectory in/var/www/html/magento2/update/app/bootstrap.php on line13
PHP Fatal error:require_once():Failed opening required'/var/www/html/magento2/update/vendor/autoload.php'(include_path='.:/usr/share/pear://usr/share/php')in/var/www/html/magento2ce/update/app/bootstrap.php on line13
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: