0

Can anyone explain the methods to doing this job done?

Thank you in advance.

1 Answer 1

1

First you need to create your cron in Cron directory

app/code/Vendor/Module/Cron/Mycron.php

<?php

    namespace Vendor\Module\Cron;
     
    class Mycron
    {
        protected $logger;
     
        public function __construct(
            \Psr\Log\LoggerInterface $loggerInterface
        ) {
            $this->logger = $loggerInterface;
        }
     
        public function execute() {
            //Code here
            //$this->logger->debug('Vendor\Module\Cron\Mycron');
    
        }
    }

then create cron_groups.xml in app/code/Vendor/Module/etc/cron_groups.xml

<?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="vendor_module_cron_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 add entry in admin

Now for scheduling create crontab.xml in app/code/Vendor/Module/etc/crontab.xml. Schedule it according to your need

<?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="vendor_module_cron_group">
    
        <job name="vendor_module_cronjob_mycron" instance="Vendor\Module\Cron\Mycron" method="execute">
            <schedule>*/5 * * * *</schedule>
        </job>
         
    </group>
</config>

This will run your cron every 5 min. your magento cron must be configured on your server or you can run manually by running php bin/magento cron:run (run twice for schedule and execute)

Note: you can skip cron_groups.xml step and define default group too as below

<group id="default">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.