If you are Looking for how to set a custom cron job and optionally a custom cron group then this post will help you understand in detail how to set up.
If you are going with a default defined Magento-provided cron group, you don’t have to define a custom cron group; however, if you want your cron jobs to run at a different schedule or you want them all to run together, you should define a cron group.
In your custom module if you need to schedule tasks periodically, you must set up a crontab for that module. A crontab is a cron job’s configuration which you have defined in that particular module.
You can optionally set up a custom group, using this among other things enables you to run cron jobs defined in that group independently of other cron jobs. Simple meaning, you can define multiple cron jobs to run all together independently using a custom cron group.
Overview of cron:
Many of Magento features require scheduled activities to occur in the future or run periodically. Few of them listed here for your reference:
- Newsletter email
- Generate Google sitemaps
- Reindexing
- Update currency rate
- Catalog price rules
Note: Magento recommends to run cron as the Magento file system owner and Do not run cron as root; You can no longer run dev/tools/cron.sh because the script has been removed.
How to configure Cron:
To configure a cron for a custom module you can follow below shared steps:
- Create a crontab.xml in your module etc folder as follows:
<Magento component dir>/<vendorname>/module-<name>/etc/crontab.xml
- File should have following content:
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config> <group id="<group_name>"> <job name="<job_name>" instance="<classpath>" method="<method>"> <schedule><time></schedule> </job> </group> </config> |
Where:
Value | Description |
group_name | Name of the cron group. The group name doesn’t have to be unique. You can run cron for one group at a time. |
job_name | Unique ID for this cron job. |
classpath | Class to be instantiated (classpath). |
method | Method in classpath to call. |
time | Schedule in cron format. Omit this parameter if the schedule is defined in the Magento database or other storage. |
Verify Cron job
- Run command to clean the Magento cache, php bin/magento cache:clean
- Enter the php bin/magento cron:run command two or three times. The first time you enter the command, it queues jobs; subsequently, the cron jobs are run. You must enter the command at least twice.
How to configure Cron group:
We can use default Magento group id in crontab.xml to run defined cron job or if need to specify custom cron group you can follow below shared steps:
- Declare a new group and specify its configuration options (all of which run in store view scope) via the cron_groups.xml file, located in:
<your component base dir>/<vendorname>/module-<name>/etc/cron_groups.xml
- File should have following content:
1 2 3 4 5 6 7 8 9 10 11 12 |
<config> <group id="<group_name>"> <schedule_generate_every>1</schedule_generate_every> <schedule_ahead_for>4</schedule_ahead_for> <schedule_lifetime>2</schedule_lifetime> <history_cleanup_every>10</history_cleanup_every> <history_success_lifetime>60</history_success_lifetime> <history_failure_lifetime>600</history_failure_lifetime> <use_separate_process>1</use_separate_process> </group> </config> |
Where:
Option | Description |
schedule_generate_every | Frequency (in minutes) that schedules are written to the cron_schedule table. |
schedule_ahead_for | Time (in minutes) in advance that schedules are written to the cron_schedule table. |
schedule_lifetime | Window of time (in minutes) that cron job must start or will be considered missed (“too late” to run). |
history_cleanup_every | Time (in minutes) that cron history is kept in the database. |
history_success_lifetime | Time (in minutes) that the record of successfully completed cron jobs are kept in the database. |
history_failure_lifetime | Time (in minutes) that the record of failed cron jobs are kept in the database. |
use_separate_process | Run this crongroup’s jobs in a separate php process |
Verify Cron group:
- Clean the Magento cache: Run bin/magento cache:clean
- Run Magento cron jobs for your custom group: php bin/magento cron:run –group=”custom_crongroup”
- Run the command at least twice.
Reference: