Magento 2 Cron Job In Custom Moudle
1. Create a class to run cron
<?php
namespace Magento\SampleMinimal\Cron;
use \Psr\Log\LoggerInterface;
class Test {
protected $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
/**
* Write to system.log
*
* @return void
*/
public function execute() {
$this->logger->info('Cron Works');
}
}
2. Create 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="default">
<job name="custom_cronjob" instance="Magento\SampleMinimal\Cron\Test" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>
The preceding crontab.xml runs the Magento/SampleMinimal/Cron/Test.php class once per minute, resulting in a row being added to the cron_schedule table.
3. Verify the cron job
php bin/magento cron:run
Reference Link: Configure a custom cron job and cron group (tutorial)