Magento 2 – Set up a custom cron job and cron group

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:

  1. Create a crontab.xml in your module etc folder as follows: <Magento component dir>/<vendorname>/module-<name>/etc/crontab.xml
  2. File should have following content:

Where:

ValueDescription
group_nameName of the cron group. The group name doesn’t have to be unique. You can run cron for one group at a time.
job_nameUnique ID for this cron job.
classpathClass to be instantiated (classpath).
methodMethod in classpath to call.
timeSchedule in cron format. Omit this parameter if the schedule is defined in the Magento database or other storage.

Verify Cron job

  1. Run command to clean the Magento cache, php bin/magento cache:clean
  2. 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:

  1. 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
  2. File should have following content:

Where:

OptionDescription
schedule_generate_everyFrequency (in minutes) that schedules are written to the cron_schedule table.
schedule_ahead_forTime (in minutes) in advance that schedules are written to the cron_schedule table.
schedule_lifetimeWindow of time (in minutes) that cron job must start or will be considered missed (“too late” to run).
history_cleanup_everyTime (in minutes) that cron history is kept in the database.
history_success_lifetimeTime (in minutes) that the record of successfully completed cron jobs are kept in the database.
history_failure_lifetimeTime (in minutes) that the record of failed cron jobs are kept in the database.
use_separate_processRun this crongroup’s jobs in a separate php process

Verify Cron group:

  1. Clean the Magento cache: Run bin/magento cache:clean
  2. Run Magento cron jobs for your custom group: php bin/magento cron:run –group=”custom_crongroup” 
  3. Run the command at least twice.

Reference:

https://devdocs.magento.com/guides/v2.3/config-guide/cron/custom-cron.html
https://devdocs.magento.com/guides/v2.3/config-guide/cron/custom-cron-ref.html

Leave a Reply

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