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="" 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 name=.../>

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="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
    <group id="custom_group_name">

And finally, create the Class and method that you specified previously, and will be executed each time /app /code /Vendor /Module /Cron /Class.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


You can download the Magento Module used in this post here:

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
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x