The Complete Magazine on Open Source

Magento Cron for dummies: Configuration, Cron jobs and more

3.75K 0

How to configure Magento cron

In both the Magento version 1 and 2, Cron job is the most amazing feature. The Cron helps the Magento 2 store owners to set up the commands which will run on system systematically at a set time and date.

Magento uses Cron Jobs to run scheduled tasks, reindexing, generating emails, newsletters, sitemaps and much more. The Cron executes the internal Magento Cron job manager and manages which jobs of Cron are executed and when.

A Magento Cron is fairly easy to configure. Just a few lines in config.XML, a method and then its done. If you want to create a schedule configurable Cron, Magento has already included in the system, and it is also pretty easy to implement.

Cron itself is more or less straightforward. It is defined in a config.XML file with two primary parts: schedule and a method to run. A sample that is mentioned below shows a cron job, namely “my_cron” that runs “doSomething” method inside the observer file in every 5 minutes.

<config>

<crontab>

<jobs>

 

<schedule>

<cron_expr>*/5 * * * *</cron_expr>

</schedule>

<run>

<model>mymodule/observer::doSomething</model>

</run>

</my_cron>

</jobs>

</crontab>

</config>

 

The main thing about this schedule is that the schedule cannot be configured yet. Inside the Mage_Cron_Model_Observer class, which generates multiple cron jobs, there is a method of producing that also checks for the values inside core_config_data table as same as it does for config.XML files. Keeping this in mind, save the path to the Cron schedule and set any valid value that you want.

Cron configuration in Magento 2 is highly important as many features of Magento 2 are highly based on scheduled activities that are possible only via Cron jobs. Here is their list:

  • Reindexing
  • Private sales
  • Automatic updating of the currency rates
  • Magento emails
  • Customers alerts and notifications
  • Generating Google Sitemaps
  • Newsletters
  • Catalog price rules
  • Log cleanup

You should run Cron as the Magento file system owner and do not run it as a root. It is not a good practice to run the Cron as the web server. It is also required to mention that in UNIX systems, scheduled tasks depends on the Crontab.

Let us talk about the Magento 2 Cron configuration.

  • It is required to perform some important tasks before the configuration.Login to the Magento 2 server as a master user, who has all the permission to write to the Magento 2 file system. You can now use the following syntax to switch to the Magento file system owner if you use the bash shell and use the following command: su <Magento 2 file system owner> -s /bin/bash -c <command>
  • For running the Magento 2 commands from any directory add You Magento 2 install dir>/bin to your system’s path. Example for Bash shell CentOS: PHPexport PATH=$PATH:/var/www/html/magento2/bin

There are other ways to run the command:

In the case of, cd <your Magento install dir>/bin, you can run this command as: ./magento <your command name>. Alternatively, php <your Magento 2 install dir>/bin/Magento <command name> where <your Magento 2 install dir> is a web servers decroot subdirectory.

To configure Cron:

    1. On the admin menu, select System->Configuration. Then on the left, in the panel under Advanced, select System.
    2. Click to expand the Cron section.
    3. Enter the number of minutes for each of the following:
      • Generate every schedule
      • Schedule ahead for
      • Missed if not run within
      • Every history cleanup
      • Success history, lifetime
      • Failure history, lifetime
    4. When complete, click the Save Configuration button.

For Web server configuration, run a phpinfo.php file in your web browser and search the loaded configuration file option.

Run Cron from the command line:

Command option, Magento Cron: run [–group=”<cron group name>”] where the –group specifies the Cron group to run. You must run Cron twice, the first time to discover tasks to run and the second time to run the task themselves.

Run Cron in the background:

This discusses how to run all Magento Cron jobs every minute, which is the recommended interval for both the Magento Community Edition and Enterprise Edition. Run Magento Cron jobs as the Magento file system owner.

Magento 2 uses Cron for two sets of tasks, and for each, Cron can run with a different configuration.

  1. PHP command line configuration- It is a general Cron job that reindexes indexers, generates e-mails, generates a sitemap, and so on. You can find the command line configuration using command php –ini.
  2. 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 configuration using command phpinfo.php.

To avoid issues during the installation and upgrade, it is strongly recommended to apply the same PHP settings to both the PHP command line configuration and the PHP web server plug-in’s configuration. In a multi-node system, Crontab can run only on one node. This applies if you set up more than one web node for the reasons related to performance or its scalability.

Creating Magento Cron job:

You must login as a user with root privileges to create a Cron job and run the following command: crontab -u <Magento 2 file system owner user name> -e

Choose the following commands, but you may need to choose a text editor first.

*/1 * * * * php -c <ini-file-path> <your Magento install dir>/bin/Magento cron:run
*/1 * * * * php -c <ini-file-path> <your Magento install dir>/update/cron.php
*/1 * * * * php -c <ini-file-path> <your Magento install dir>/bin/magento setup:cron:run

The first command reindexes the indexers, sends automated e-mails and generates the sitemap. Basically the command is associated with the PHP command line .ini file. Other two commands are used for the Component Manager and System Upgrade. For example, if the PHP is located in /user/bin you installed Magento 2 in /var/www/html/Magento2 and all the commands used the servers php.ini.
Example: */1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/bin/magento cron:run > /var/www/magento2/var/log/magento.cron.log&
*/1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/update/cron.php > /var/www/magento2/var/log/update.cron.log
*/1 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/magento2/bin/magento setup:cron:run > /var/www/magento2/var/log/setup.cron.log&

Now save all the changes to the Crontab.

Magento Cron groups configuration for Custom Module:

If you wish to implement Cron for the custom module Magento, it allows you to use the default group or the different group. For the configuration of Cron group for your module, create <your magento2 component base dir>/<vendorname>/module-<name>/etc/crontab.xml with the following code:

<config>
<group id=”<group_name>”>
<job name=”<job_name>” instance=”<classpath>” method=”<method>”>
<schedule><time></schedule>
</job>
</group>
</config>

  • <group_name> is the Cron group name, you can only Cron for single group at a time.
  • <job_name> is the unique ID used for identifying the Cron job.
  • <classpath> the identify name for itself.
  • <method> indicates a method those call in the classpath.
  • <time> is scheduled in Cron format.

Adding a new Magento Cron task via SSH

Login to the server via SSH. Run the crontab e command to add a cron task. After this, you will see a text editor, where you can add or edit Cron tasks. Starting from a new line, add the following record:

*/5 * * * * sh /path/to/your/magento/site/root/cron.sh.

Do not forget to add your own default folder path. Save all the changes and then close the file. If you did everything correctly, the crontab-l command will show you the newly created task.

Adding a new Magento Cron task via cPanel

To set up a new Magento Cron task via cPanel, login to your site’s configuration panel. Then, enter Cron into the search field and you’ll see a Cron jobs option in the advanced section. To set up Cron job with this method, you may need to enter an email address in the Cron email section. The email will be used to send Cron messages which are useful in any case of Cron job execution issues.

Check Magento Cron status

To make sure that the Cron task works correctly, you can go and see the changes that appear in the cron_schedule table of Magento database. If the contents are changing, then you configured Cron in the right way.

Using email address for Cron

Make sure to choose the correct email address in MAILTO. If there’s an issue with the Cron task execution, you will receive an email with its error description, which in the case of troubles will help you to understand that what went wrong.

Here is how to set the Cron for Magento 2 and add an email address for receiving the messages:

MAILTO=”[email protected]
*/5 * * * * php /path/to/magento/bin/magento — –quiet cron:run
Do not forget to replace the example address with your own email and insert the correct path to your Magento default folder.

Common mistakes of Magento Cron configuration

The most common mistake while setting the Cron on any website are due to copying instructions from the internet. Those instructions are correct but need adaptation for the server or were correct at the moment when the article was written but is not correct anymore.

Hope, the article was helpful for you. If you still got any queries or didn’t fully understand, do write us back.