How to Set up a Custom Cron Job in Magento 2

Firstly, we all know the Magento 2 and its features and how best technology it is.

Similarly, Magento 2 creates a cron job for performing an automatic schedule at a particular time.

This cron job helps to run the set of commands at a specific time and date simlilarly. However, setting up a custom cron job in Magento 2 is still a question.

Therefore, today we have listed the steps for creating and setting up a custom cron job in Magento 2. But, first, let's check what is a corn job?

What is a cron job?

Cron Job feature is provided by Linux operating system. For instance, it works as scheduling a utility present in the system.

This cron job creates a relevant script or command that you want to perform.

Moreover, it runs in the background and runs a set of commands at a specific time again and again. On the other hand, it reduces the manual working of running a command repeatedly.

Above all, a cron job is the best choice for those who often have to do the same task every day.

For instance, some tasks or activities of the websites might require actions at a specific time, yet you can't deal with that physically. Therefore making a Cron Job on your web server becomes helpful.

Moreover, you can use corn configuration for scheduling various activities like Magento emails, reindexing, auto-update of currency rates, etc. However, make sure that the configuration is correct to activate the corn job.

Above all, if the cron job is not configured perfectly, your function will not perform as expected, representing an error.

How to Set up a custom Cron Job in Magento 2

This technique helps store owners to handle the caches and keep the index up to date. In addition, some activities should be completed later to keep away from system over-burden or just because of the necessity of condition satisfaction.

Step 1: Firstly, create a registration.php file at app\code\[Vendor]\[Namespace]\. Then, paste the below code to the file.

<?php
        \Magento\Framework\Component\ComponentRegistrar::register(
            \Magento\Framework\Component\ComponentRegistrar::MODULE,
            '[Vendor]_[Namespace]',
            __DIR__
        );

Step 2: After that, Create a module.xml file at app\code\[Vendor]\[Namespace]\etc and paste the following code.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="[Vendor]_[Namespace]" setup_version="1.0.0"/>
</config>

Step 3: Now, create a crontab.xml file at app\code\[Vendor]\[Namespace]\etc. After that, add 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 instance="[Vendor]\[Namespace]\Cron\Reminder" method="execute" name="vendor_namespace_cron">
                <schedule>0 4 * * *</schedule> <!-- here you need to schedule time to run cron job -->
            </job>
        </group>
    </config>

Step 4: Lastly, create a Reminder.php file at app\code\[Vendor]\[Namespace]\Cron. And paste the below code.

<?php
 
    namespace [Vendor]\[Namespace]\Cron;
 
 
    class Reminder
    {
        
        public function execute()
        {
 
            /*
            *
            * Here you need to define logic
            *
            */
            return $this;
        }
    }

That's all you need to do to set up a custom cron job in Magento 2.

Wrapping Up

In conclusion, the cron job is already known for its automation. In addition, it helps you make your repetitive task easy and utilize your time for some other work in the same vein.

Further, you can manage all those activities you need to perform at a specific time and date. Most importantly, advance your store functionalities by using cron job automation.

Lastly, if you face any difficulties, let us know in the comment section or contact us directly.

To clarify more, reach us for any issue regarding eCommerce, for instance hosting or eCommerce website, or digital marketing services.

You can choose the best domain and hosting service provider as per the e-commerce platform you choose.

If you are using Magento, which is recommended, you must try using Nexcess Server and the hosting.

You can also check with alternative hosting such as Hostinger, which has some excellent plans, and their service is world-class.

Hoping to help you all further!

Stay Tuned :-)