A cron job is a task scheduled by script or command to run automatically at certain intervals.
1) app/code/Jigar/CronExample/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="default">
<job name="jigar_cronexample_customcron" instance="Jigar\CronExample\Cron\Customcron" method="execute">
<schedule>*/5 * * * *</schedule>
</job>
</group>
</config>
2) app/code/Jigar/CronExample/Cron/Customcron.php
<?php
declare(strict_types=1);
namespace Jigar\CronExample\Cron;
class Customcron
{
protected $logger;
/**
* Constructor
*
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Execute the cron
*
* @return void
*/
public function execute()
{
$this->logger->addInfo("Cronjob customcron is executed.");
}
}
Leave a Comment