It is through Cron Job where a time-based scheduler is possible to execute, that is, Magento 2 can automatically run commands given the schedule set. Technology has its way of making our lives easier and when it comes to making use of Magento 2, the Cron Job is becoming the crowd’s favorite. Activities such as generating Google sitemaps, reindexing, customer alerts, updating of currency rates and a lot more can be done within the schedule. As for now, we will guide you on how to successfully set-up Cron Job in Magento 2.
Let’s start by creating crontab.xml in app/code/sparxbpo/Mymodule/etc directory then paste this code in it, where in this illustration, the scheduled duration is for one minute, the group id default and job name as cron_name:
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:module:Magento_Cron:etc/crontab.xsd”>
<job name=”cron_name” instance=”sparxbpo\Mymodule\Cron\Run” method=”execute”>
<schedule>*/1 * * * *</schedule>–>
Next step is to create Run.php by making a directory Cron in your custom module. This will then like this: app/code/sparxbpo/Mymodule/Cron. Create Run.php file in app/code/sparxbpo/Mymodule/Cron where the following code will be pasted:
public function __construct(
$this->_logger = $logger;
public function execute()
//Edit it according to your requirement
$this->_logger->debug(‘Cron run successfully’);
To run Cron Job in Magento 2, connect your store with an SSH. Go to the root directory of your Magento 2and run the commands below:
php bin/magento cache:flush
php bin/magento cron:run
Another option is to run Cron job for one group only by running this command:
php bin/magento cron:run –group=”your_group_name”.
The last step of the entire process is to check if you succeed in setting up Cron Job. To do this, go to var/log/debug.log of your store where you will see the text Cron run successfully in it. Then, go to cron_schedule table of database and you will see a new entry in it with your chosen Cron job name.
Sparx BPO, A Total Magento 2 development Company based on Ahmedabad, India. we offer full-services Magento 2 Theme Development, Magento 2 Migration Services, Magento 2 Maintenance Services, Magento 2 SEO services, eCommerce portal development and hire magento 2 developers.
You May Also Like: