Running cron jobs in Magento 2

cron_job

When it comes to Magento 2 and cron jobs, there are some improvements comparing to Magento 1.

It is still abstracted in the way that you don’t really run jobs directly, instead, you use cron syntax to setup periodical method execution via Magento scheduler that works on top of system’s cron utility.

Here are some useful hints to help you get started with setting up your own.

Database doesn’t seem to be changed when we compare it with Magento 1 but configuration does.
Magento 2 will execute entries from cron_schedule table just like in Magento 1.

News in configuration is that now we are able to group our jobs and chose if we want our jobs to be executed in parallel as separate processes which could come very handy sometimes.
magento2-cron-configuration

So, in order to run scheduled tasks in Magento, first we need to make sure that we configure the following jobs to run via system cron. This is most simple way to do it:

*/1 * * * * /path/to/php /path/to/m2/installation/bin/magento cron:run [>> /log/file &]
*/1 * * * * /path/to/php /path/to/m2/installation/update/cron.php [>> /log/file &]
*/1 * * * * /path/to/php /path/to/m2/installation/bin/magento setup:cron:run [>> /log/file &

At this point you have all those M2 default jobs ready to go!

So, how to make your own job?

More or less, this is done in the similar way as in Magento 1.
First you need crontab.xml inside your custom module:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default"> <!-- Doesn't need to be unique -->
        <job name="cron_example" instance="Inchoo\CronExample\Cron\Example" method="execute">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

Configuration above is to make sure that every minute, Inchoo\CronExample\Cron\Example:execute() is run which would (according the code below log it’s name every minute.)

<?php
namespace Inchoo\CronExample\Cron;
class Example {
 
    protected $_logger;
 
    public function __construct(\Psr\Log\LoggerInterface $logger) {
        $this->_logger = $logger;
    }
 
    public function execute() {
        $this->_logger->info(__METHOD__);
        return $this;
    }
}

It is also worth mentioning that you could chose to run only one job group at the time.
Here is how you do it:

magento cron:run [--group="cron_group_name"]

Your thoughts appreciated

I hope this helps you with your cron jobs setup – how would you go about this and do you have some tips and tricks to share?


About Tomas Novoselic

Team Leader/Backend Developer

At Inchoo, Tomas is a Team Leader and Certified Magento Developer who handles Magento modifications at any level. He also works closely with clients on Magento projects of any size and difficulty.

Read more posts by Tomas / Visit Tomas's profile

6 comments

  1. Hi,

    I did exactly as is said in the article above. When I ran $ magento cron:run, I could see the execute entry in var/log/system.log file. But that’s it. The cron job did not auto run after that. In crobtab.xml it is set to run every minute. And In group:default configuration, it is set to run every 15 minutes. But it has not run in either case. What could be going wrong ?

    Thanks,
    Azher

  2. Hi,

    I meet this issue “update-cron.ERROR: Cron readiness check failure! Found following non-writable paths [] []” when I try to run cron in my O.S Ubuntu hosted in AWS Amazon. I gave permission access but nothing change. Please can you help me to find a solution?

  3. If you do setup your own cron group (e.g., you use something other than “default”) then you will need to create a cron_group.xml file in your extension. Search the Magento codebase for other “cron_group.xml” files to determine the pattern.

  4. Just for information: If you want to run cron job directly without having to wait for the schedule (e.g when testing), you can use

    n98-magerun2 sys:cron:run <job name>

    (same way as Magento 1)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.