:::: MENU ::::

Magento Tutorial | Magento Blog | Learn Magento 2

Cookies Consent Popup

First you need to create your cron file in `Cron` directory as below


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() {

            //Your Logic/Code here

            //$this->logger->debug('Vendor\Module\Cron\Mycron');

          }

        }


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

 it's option step.


    <?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 cron script create crontab.xml on below path

`app/code/Vendor/Module/etc/crontab.xml`. 

Schedule time according to your need. I configured for every 5 minute.


    <?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 execute your cron at every 5th 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">


0 comments:

Post a Comment