How do you create a custom cron job in Magento 2 and ensure it executes only once per cycle?

Here crontab.xml file

<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="vendor_module_cronjob" instance="VendorModuleCronCustomJob" method="execute">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

<schedule>0 3 * * *</schedule>

0 – Minute (0th minute, or the beginning of the hour)
3 – Hour (3 AM)
* – Day of the month (every day)
* – Month (every month)
* – Day of the week (every day of the week)

As per your code it’s running every minute. To adjust the schedule in crontab.xml, you can modify <schedule>* * * * *</schedule> to suit your timing requirements. The format follows standard cron syntax:

* * * * * command to be executed
| | | | |
| | | | +----- Day of week (0 - 7) (Sunday=0 or 7)
| | | +------- Month (1 - 12)
| | +--------- Day of month (1 - 31)
| +----------- Hour (0 - 23)

Leave a comment

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