Running Cron Jobs in Magento 2

Running Cron Jobs in Magento 2 can be very useful to automate repetitive tasks. A cron is a Linux utility which schedules a command or script on your server to run automatically at a specified time and date.

Setting up crontab in our machine

First of all, to run Cron Jobs in Magento 2 you have to set up the Magento 2 cron commands in your crontab service.

joki@jokiruiz:~$ sudo crontab -u www-data -e

If your apache2 or nginx user is not www-data change it, and insert in your cron the following lines:

* * * * * /usr/bin/php /var/www/magento-two/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/magento-two/var/log/magento.cron.log

* * * * * /usr/bin/php /var/www/magento-two/update/cron.php >> /var/www/magento-two/var/log/update.cron.log

* * * * * /usr/bin/php /var/www/magento-two/bin/magento setup:cron:run >> /var/www/magento-two/var/log/setup.cron.log

If your php directory is not /usr/bin/php or your magento directory is not /var/www/magento-two/ replace them for your php and magento paths.

Adding a custom cron to our module.

First of all, create a file ‘crontab.xml‘ inside the etc folder of your module /app /code /Vendor /Module /etc /crontab.xml.

Here you have to specify a group_id (can be a default group or a custom group), and the job name. Inside the job, you have to set the instance of the class and the method you want to execute, and finally the schedule in crontab format.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="custom_group_name">
        <job name="custom_job_name" instance="Vendor\Module\Cron\Class" method="method">
            <schedule>*/30 * * * *</schedule>
        </job>
        <job name=.../>
    </group>
</config>

Example of crontab syntax

┌───────────── min (0 - 59)
│ ┌────────────── hour (0 - 23)
│ │ ┌─────────────── day of month (1 - 31)
│ │ │ ┌──────────────── month (1 - 12)
│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
│ │ │ │ │ Saturday, or use names; 7 is also Sunday)
│ │ │ │ │
│ │ │ │ │
* * * * * command to execute

Then, if you specified a custom group, create the file ‘cron_groups.xml‘ inside the same folder /app /code /Vendor /Module /etc /cron_groups.xml

Here you can specify the timings for the cron generation and the history lifetime times.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
    <group id="custom_group_name">
        <schedule_generate_every>1</schedule_generate_every>
        <schedule_ahead_for>4</schedule_ahead_for>
        <schedule_lifetime>2</schedule_lifetime>
        <history_cleanup_every>10</history_cleanup_every>
        <history_success_lifetime>60</history_success_lifetime>
        <history_failure_lifetime>600</history_failure_lifetime>
        <use_separate_process>1</use_separate_process>
    </group>
</config>

And finally, create the Class and method that you specified previously, and will be executed each time /app /code /Vendor /Module /Cron /Class.php

<?php
namespace Vendor\Module\Cron;

class Class
{
    protected $_logger;

    public function __construct(\Psr\Log\LoggerInterface $logger)    
    {
        $this->_logger = $logger;
    }

    public function method() {
        $this->_logger->info('Executing cron “method”');
    }
}

Editing the custom cron group options

Magento 2 gives you the ability to change the group configuration. You can change it in Configuration > Advanced > System > Cron configuration options.

Running Cron Jobs in Magento 2 - configuration
Magento 2 Cron Configuration

Check the cronjob queue on the database

And finally, if you want to check if the cron job has been executed or it is scheduled, you can check in the database using next query:

SELECT * FROM magento_two.cron_schedule;
Running Cron Jobs in Magento 2 - database
Magento 2 cron schedule table

Source

You can download the Magento Module used in this post here: https://github.com/JoaquinRuiz/Magento2-Cron-Tutorial

I hope you enjoyed the post about Running Cron Jobs in Magento 2, and helps you with your cron jobs setup. Do you have any tricks to share? Please comment!

Magento 2 rules ! 😀

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x