The cron job will create a command or a script that is appropriate with the task you want to do. Instead of manual working, the cronjob allows running automatically in exact time and date. Due to its automation, the cron jobs is the perfect choice for repeated projects every date or every week.
Follow the below steps to create crojob
Create crontab.xml
File: app/code/PHPCodez/First/etc/crontab.xml
Content would be
<?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 instance="PHPCodez\First\Cron\First" method="execute" name="phpcodez_first_cron"> <schedule>* * * * *</schedule> </job> </group> </config>
group id is your cron group name. You can run only cron for single group at a time.
job instance is class to be instantiated (classpath).
job method is method in job instance to call.
job name is Unique ID for this cron job.
schedule is schedule in cron format.
Create First.php
File: app/code/PHPCodez/First/Cron/First.php
<?php namespace PHPCodez\First\Cron; class First { public function execute() { $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/cron.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info(__METHOD__); return $this; } }
Issue the following commands
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:clean
Thanks for sharing, I was having issue while fixing cron issue in my Magento store, https://www.cloudways.com/blog/setup-magento-cron-job/, your and this post helped me a lot.