Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Here We Learn How To Setup Cron Job in Magento2

    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

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

    php bin/magento cron:run
    
    /* from magento root */
    
    

    thanks 🙂

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    1 comments

  • brstdev3
    Hello Abhishek,

    Thanks for the post.
    Can we set more then one cron in a single module? or how we can set more then one cron.
    Please let me know as i want to add more then one cron in my module.

    thanks

  • Back to Top