Magento 2 – Custom Module Cron Job Not Running

cronmagento2module

I'm working on a custom module to export any new orders periodically throughout the day. I've read through countless descriptions of how to set up cron with for a custom module and it seems like I'm doing everything right, but yet it doesn't seem to be doing anything. The module is showing as enabled in admin and the default cron jobs are running. I'm using a custom name for my module's cron group, but that group does not show up when I attempt to configure it under Stores > Configuration > Advanced > System and my job never shows up on the cron_schedule table.

Here is my vendor/module/etc/crontab.xml:

<?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="custom_group">
        <job name="custom_name" instance="<vendor>\<module>\Cron\Test" method="execute">
            <schedule>*/5 * * * *</schedule>
        </job>
    </group>
</config>

and my vendor/module/Cron/Test.php

<?php
namespace spinst\OrderExport\Cron;
use \Psr\Log\LoggerInterface;

class Test {
    protected $logger;

    public function __construct(LoggerInterface $logger) {
        $this->logger = $logger;
    }

    public function execute() {
        $this->logger->info('Cron Works');
    }

}

Does anyone have any tips as to what I could be doing wrong?

Best Answer

You need to do one of the following options:

1) Use <group id="default"> in vendor/module/etc/crontab.xml

or

2) Create your own vendor/module/etc/cron_groups.xml with the following:

<?xml version="1.0"?>
<!--
/**
 * path:
 * magento_home/app/code/vendor/module/etc/cron_groups.xml
 */
-->
<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">
        <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>

This will then show up in your backend Stores->Configuration->Advanced->System under Cron.

To test : php bin/magento cron:run --group="custom_group"