There are a lot of processes in Magento 2 admin that require automation and scheduling. Updating currency rates, sending newsletters and product alerts are all handled by a handy default feature — Magento cron jobs.
Besides, plenty of Magento 2 extensions also come with custom cron jobs to execute different tasks within the module. So, if you're wondering how you can create one for your extension too, you've landed in the right place.
Today you'll learn how to create a cron job in Magento 2 step by step.
Post Contents [hide]
Note: before creating your custom cron job, make sure you have the crons by opening the crontab as the Magento file system owner. Run the following command:
crontab -l
The result should be as follows:
#~ MAGENTO START c5f9e5ed71cceaabc4d4fd9b3e827a2b
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /var/www/html/magento2/var/log/magento.cron.log
#~ MAGENTO END c5f9e5ed71cceaabc4d4fd9b3e827a2b
1. Create a Simple Module
The first step to create a custom cron job in Magento is to create a basic module. If you already have one, move to the next step.
2. Create a Class
Move to your custom module root directory and create a cron directory there.
e.g. app/code/VendorName/ModuleName/Cron
Then create a file named Cron/SomeCronModel.php with the following code:
<?php
namespace VendorName\ModuleName\Cron;
class SomeCronModel
{
public function execute()
{
//your cron job code
}
}
The class you create will write a row in the cron_schedule table in the database to confirm that the cron job has been run successfully.
3. Create a crontab.xml File
Add the etc/crontab.xml file (configurable cron job file) in the cron directory with the following code:
<?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="vendorname_modulename_cron_somecronmodel" instance="VendorName\ModuleName\Cron\SomeCronModel" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>
default — the ID of the group your cron job will be included in. We recommend adding it to the default group
vendorname_modulename_cron_somecronmodel — the name of your cron job. It must be unique, so we recommend creating it from the name of the cron job model
VendorName\ModuleName\Cron\SomeCronModel — the name of the cron job model
execute — the name of the executed method in the cron job model
* * * * * — the frequency of the cron job.
4. Compile Code
Once you finish creating a cron job in your Magento 2, you need to compile the code. Run the following command:
bin/magento setup:di:compile
Additionally, clean cache:
bin/magento cache:clean
5. Test Cron Job
To make sure your custom Magento cron job works properly, test it using SQL query on the cron_schedule table. Run the cron job manually, using the following command:
bin/magento cron:run
If you use Magento 2 Cron Schedule Extension, you can also execute your cron from the admin panel. Simply navigate to System > Cron > Cron Jobs and find your custom cron job listed in the grid.
Tick it and select the Execute command from the mass action to run your cron.
Once it is executed, you can see that in the Schedule Log along with all other crons.
Congrats! You now know how to create custom cron jobs in Magento 2. However, note that creating and managing them are two completely different things.
So you need to know how to check the cron job status and execute them manually if something goes wrong.