Configure And Run Custom Cron Job In Magento 2
Cron is a Linux utility that schedules a command or script on a server to be run automatically at a specified time and date. Cron jobs are very useful to automate recurring tasks.
Generally, the syntax to write a cron is
* * * * * /etc/cron.daily/script.sh
Here,
# ┌───────────── minute (0 – 59)
# │ ┌───────────── hour (0 – 23)
# │ │ ┌───────────── day of the month (1 – 31)
# │ │ │ ┌───────────── month (1 – 12)
# │ │ │ │ ┌───────────── day of the week (0 – 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute
/etc/cron.daily/script.sh – file to be executed.
This job will run every minute.
Generally, Magento2 requires at least one cron to schedule important activities. Some of those activities are:
- Managing Catalog Price Rules
- Managing Newsletters
- Generating Google Sitemaps
- Updating Currency Rate automatically
- Alerts / Notifications to customers on the front-end (Updates in the catalog)
- Automatic Reindexing
- Private Sales (Magento Enterprise Edition only)
- Sending all types of Magento emails, including Order Confirmation and Transactional emails
It’s easy to configure a custom cron job in Magento 2. Here, I am going to explain it. Following are the steps:
- Creating and registering a new Custom Module
- Defining a Cron in Custom Module
- Creating a file associated with Cron
- Running Magento Commands
CREATE AND REGISTER A CUSTOM MODULE
To create a custom module, the very first step is to create a file, module.xml in folder <Magento Root Folder>/app/code/<Vendor Name>/<Module Name>/etc/.
Let’s take the path /app/code/Bizspice/Example/etc/module.xml and paste the following code into this file:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Bizspice_Example" setup_version="1.0.0"> </module> </config>
Now, create a file registration.php in /app/code/Bizspice/Example/ folder with the following code:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bizspice_Example', __DIR__ );
DEFINE A CRON IN THE CUSTOM MODULE
To define a custom cron, create a file crontab.xml in /app/code/Bizspice/Example/etc/ folder with the following code:
<?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="bs_cron" instance="Bizspice\Example\Cron\Run" method="execute"> <schedule>*/2 * * * *</schedule> </job> </group> </config>
There is a group tag having id default. We can also give our own group id, and multiple crons can be assigned to that group.
There is a job name bs_cron which denotes the cron defined here. When this cron runs, Bizspice\Example\Cron\Run class will be executed.
The Schedule tag has the following:
*/2 * * * *
As I have already mentioned above about the syntax of writing a cron job, this cron will run after every 2 minutes because we wrote */2 here. On the other hand, if we write only *, then this cron will run every minute.
CREATE A FILE ASSOCIATED WITH CRON
Now, create a class that has been given under the instance attribute of the job tag in crontab.xml. Create a file Run.php in the app/code/Bizspice/Example/Cron/ folder with the following code:
<?php namespace Bizspice\Example\Cron; class Run { protected $_logger; public function __construct(< \Psr\Log\LoggerInterface $logger) { $this->_logger = $logger; } public function execute() { $this->_logger->info('Bs_Cron has been run successfully'); return $this; } }
Once you are done with this, you will let Magento 2 know that you have done some custom work, and you will need to upgrade and compile your work. To do this:
RUN MAGENTO COMMANDS:
To install the custom plugin:
php bin/magento setup:upgrade; php bin/magento indexer:reindex; php bin/magento setup:di:compile; php bin/magento setup:static-content:deploy -f; php bin/magento cache:clean; php bin/magento cache:flush; rm -rf pub/static/* var/* generated/*; chmod 0777 var/ pub/ generated/; chmod 0777 -R var/ generated/ pub/;
To run cron manually:
- To run all crons:
- php bin/magento cron:run
- To run crons added under a group:
- php bin/magento cron:run –group=”your_group_id”
- In our case, group id is default. So, command will be:
- php bin/magento cron:run –group=”default”
You can check if cron has been run successfully by going to <root folder of store>/var/log/system.log file. You will see the text Bs_Cron has been run successfully in this file. Also, you can check it in the database by opening cron_schedule table, and you will see a new record with cron job name – bs_cron.
So, this is all about configuring a custom cron Magento2!!