Back to Top

Here We Learn How To Setup Cron Job in Magento2

Updated 20 February 2024

Today Here We Learn How To Setup Cron Job in Magento2

Step 1# : 1st we’ll create crontab.xml in app/code/NameSpace/Module/etc folder

<?xml version="1.0"?>
<!--
/**
 * @category  Webkul
 * @package   Webkul_ModuleName
 * @author    Webkul
 * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <!-- instance : contain class name which function we want to run on defined time -->
        <!-- method : contain method name which run on defined time -->
        <job name="custome_cron_job_name" instance="NameSpace\ModuleName\Cron\CustomClassName" method="execute">
            <schedule>*/30 * * * *</schedule> <!-- here we define time that when function execute  -->
        </job>
    </group>
</config>

Step 2# Now we create class with method which  define in instance attribute of  crontab.xml file

For this we create CustomClassName.php in app/code/NameSpace/Cron/ folder

<?php

/**
 * @category  Webkul
 * @package   Webkul_ModuleName
 * @author    Webkul
 * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

namespace NameSpace\ModuleName\Cron;

/**
 * custom cron actions
 */
class CustomClassName
{
    public function execute()
    {
        // do your custom code as your requirement
    }
}

step3# after this you need to run following command from terminal

Searching for an experienced
Magento 2 Company ?
Find out More

so open ssh console and run following command from magento root folder

php bin/magento cron:run

/* from magento root */

thanks 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


1 comments

  • brstdev3
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home