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
1 2 3 4 5 6 7 8 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?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."); } } |