📝 Community Note The content on this page was generated with the assistance of AI and is pending a human review. While we've done our best to ensure accuracy, there may be discrepancies or areas that could be improved.
cron_jobs.xml
Reference Documentation
Introduction
This reference documentation provides an in-depth explanation of the cron_jobs.xml file in Magento 2. Cron jobs are scheduled tasks that are executed at predefined intervals. The cron_jobs.xml file allows you to configure and manage these cron jobs in Magento 2.
File Location and Structure
The cron_jobs.xml file is located in the app/code/*Vendor*/*Module*/etc
folder of your Magento installation. It
follows the XML file format and defines the configuration for cron jobs.
Here is an example of the file structure:
<?xml version="1.0"?><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="job_name" instance="*Vendor*\*Module*\Cron\*CronClass*" method="execute"> <schedule>*schedule_expression*</schedule> </job> </group></config>
File Components
The cron_jobs.xml file consists of the following components:
config
Root Element
1. The config
root element encloses the entire XML configuration. It includes the xmlns:xsi
attribute, which specifies
the XML namespace for the xsi
prefix, and the xsi:noNamespaceSchemaLocation
attribute, which points to the cron jobs
XSD schema location.
group
Element
2. The group
element groups related cron jobs together. It has a required id
attribute that identifies the group.
job
Element
3. The job
element defines a single cron job within a group. It has the following attributes:
-
name
: Specifies the unique name of the cron job. -
instance
: Specifies the fully qualified class name of the cron job's implementation class. -
method
: Specifies the method of the cron job's implementation class that should be executed.
schedule
Element
4. The schedule
element specifies the cron expression or schedule for the job. It defines when the cron job should be
executed. The *schedule_expression*
should follow the cron syntax.
Example
Let's consider an example to illustrate the usage of the cron_jobs.xml file. Assume we have a module
named Vendor_Module
with a cron job that needs to run every hour.
- Create the
cron_jobs.xml
file in the module'setc
folder with the following content:
<?xml version="1.0"?><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_cron" instance="Vendor\Module\Cron\MyCronClass" method="execute"> <schedule>0 * * * *</schedule> </job> </group></config>
-
In the above example, the
group
element has anid
attribute set todefault
. Thejob
element has aname
attribute set tovendor_module_cron
, aninstance
attribute set toVendor\Module\Cron\MyCronClass
, and amethod
attribute set toexecute
. Theschedule
element specifies that the cron job should run every hour at the 0th minute. -
In your PHP code, create the implementation class
MyCronClass
underVendor\Module\Cron
:
<?phpnamespace Vendor\Module\Cron; class MyCronClass{ public function execute() { // Perform the required cron job tasks here }}
- With the above configuration and code, the
execute()
method of theMyCronClass
class will be executed every hour.
Conclusion
The cron_jobs.xml file allows you to configure and manage cron jobs in Magento 2. By following the structure and syntax described in this documentation, you can define cron jobs and specify their schedules. Remember to provide the necessary implementation class and method for each cron job.