Magento 2 How to Create Cron Job

Setting a cron job is simple in Magento 2. Creating a cron job is like setting an automatic schedule on time, that is ready when we will be running Magento2 store. Why is it such useful function to have cron job? For example, you need assistance from a software like website statistics or content management system which requires to run at the certain time in case of your unavailability to perform this data collect operation. That is why and where you must create a cron job on your web server.

What is a Cron Job?

Cron job is an important component for correct performance. Cron job is a great feature by Linux, the free operating system for the user. 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 cron job allows running automatically in exact time and date. Due to its automation, the cron job is the perfect choice for repeated projects every date or every week. It is widely used for running actions that are performed on schedule, such as indexing and caching, sitemap generation, currency rate updates, and many more as well.

Cron configuration is very important in Magento to set the schedule for many system activities such as reindexing, an auto-update of currency rates, for Magento emails, etc. Only when the configuration is correct, the cron job is active. Cron job can fail only in the case of an error occurred on server means Magento won’t function as expected.

Why we need a Cron Job?

Several Magento features require at least one Cron Job to run, which schedules activities to occur in the future. A partial list of these activities as follows:

  • Catalog price rules
  • Newsletters
  • Reindexing
  • Customer Alerts/Notifications(product price change, product back in stock)
  • Automatic updates of currency rates
  • Generating Google Sitemaps
  • Private sales (Magento Commerce only)
  • All Magento e-mails (including order confirmation and transactional)

Run and Create cron job:

You can follow the given steps to create a cron job:

  • Create a class within the “Cron” folder
  • Manually setup the cron schedule by using PHP: bin/magento cron:run
  • Find a log in the var/log/system.log after the cron job has run.
  • Login to Magento 2 Admin panel, do as the path: Stores > Configuration > Advanced > System, then changing scheduler settings per cron group.
  • Finally, run cron from the command line:

magento cron:run [--group="<cron group name>"]

Create crontab.xml

File: app/code/Magemeta/Test/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 instance="Magemeta\Test\Cron\Test" method="execute" name="magemeta_test_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. The following graph shows what it consists of:
* * * * * command to be executed
| | | | |
| | | | +----- Day of week (0 - 7) (Sunday=0 or 7)
| | | +------- Month (1 - 12)
| | +--------- Day of month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)

In crontab.xml, we have defined job instance is Magemeta\Test\Cron\Test. It should create now.

Create Test.php

File: app/code/Magemeta/Cron/Cron/Test.php

<?php

namespace Magemeta\Test\Cron;

class Test
{

	public function execute()
	{

		$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/cron.log');
		$logger = new \Zend\Log\Logger();
		$logger->addWriter($writer);
		$logger->info('Custom cron job created in '.__METHOD__);
		return $this;

	}
}

Now flush the cache and run magento cron:run --group="default" from the command line.

To check whether the Cron is working properly or not, go to var/log/cron.log of your store and you will see the text Magemeta\Test\Cron\Test::execute in it.

Leave a Reply