How to override cron job group in Magento 2?

You can see your existing cron Group name from your module crontab.xml file.

Override the Module Cron Job group from default to custom group easily. Also, you can change the cron schedule time.

If you want to override any existing Crom Job that contains a default Group, You can change it to your custom group by creating a simple module.

You need to create a simple module with the etc/module.xml and registration.php file.

To Override cronjob, You have to create one extra file under the, etc folder with the etc/crontab.xml file.

<?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="my_custom_group">
        <job name="product_feed" instance="Rbj\Feed\Model\DataFeed" method="generate">
            <schedule>0 0 * * *</schedule>
        </job>
    </group>
</config>

Here job name should be the same as your old module contains. product_feed is the Job name and that will be unique among all the Crons of the system.

Now You can define the cron_groups.xml file to define basic cron-related stuff.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
    <group id="my_custom_group">
        <schedule_generate_every>5</schedule_generate_every>
        <schedule_ahead_for>15</schedule_ahead_for>
        <schedule_lifetime>10</schedule_lifetime>
        <history_cleanup_every>600</history_cleanup_every>
        <history_success_lifetime>600</history_success_lifetime>
        <history_failure_lifetime>900</history_failure_lifetime>
        <use_separate_process>1</use_separate_process>
    </group>
</config>

You can see your custom cron groups from the admin panel and able to manage them.

  1. Log in to the Admin panel.
  2. Click Stores -> Settings -> Configuration -> Advanced -> System.
  3. Check Cron Schedule Section,

To verify your custom group:
Run Magento cron jobs for your custom group

php bin/magento cron:run --group="my_custom_group"
php bin/magento cache:flush

You can see your job scheduled from the cron_schedule table in a database.