Step 2: Verify the sample module
Before you continue, verify that the sample module is registered and enabled.
-
Run the following command:
bin/magento module:status Magento_SampleMinimal
-
Make sure that the module is enabled.
Module is enabled
Module does not exist
, review Step 1 carefully. Make sure your code is in the correct directory. Spelling and case are important; if anything is different, the module will not load. Also, do not forget to run magento setup:upgrade
.Step 3: Create a class to run cron
This step shows a simple class to create a cron job. The class only writes a row to the cron_schedule
table that confirms it is set up successfully.
To create a class:
-
Create a directory for the class and change to that directory:
mkdir /var/www/html/magento2/app/code/Magento/SampleMinimal/Cron && cd /var/www/html/magento2/app/code/Magento/SampleMinimal/Cron
-
Created a file named
Test.php
in that directory with the following contents:<?php namespace Magento\SampleMinimal\Cron; use Psr\Log\LoggerInterface; class Test { protected $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * Write to system.log * * @return void */ public function execute() { $this->logger->info('Cron Works'); } }
Step 4: Create crontab.xml
The crontab.xml
file sets a schedule to run your custom cron code.
Create crontab.xml
as follows in the /var/www/html/magento2/app/code/Magento/SampleMinimal/etc
directory:
<?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="custom_cronjob" instance="Magento\SampleMinimal\Cron\Test" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>
The preceding crontab.xml
runs the Magento/SampleMinimal/Cron/Test.php
class once per minute, resulting in a row being added to the cron_schedule
table.
In order to make the cron schedule configurable from the Admin, use the configuration path of your system configuration field.
<?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="custom_cronjob" instance="Magento\SampleMinimal\Cron\Test" method="execute">
<config_path>system/config/path</config_path>
</job>
</group>
</config>
Where, system/config/path
is a system configuration path defined in etc/adminhtml/system.xml
of a module.
Step 5: Compile and cache clean
Compile the code with this command:
bin/magento setup:di:compile
And clean the cache with this command:
bin/magento cache:clean
Step 6: Verify the cron job
This step shows how to verify the custom cron job successfully using a SQL query on the cron_schedule
database table.
To verify cron:
-
Run Commerce cron jobs:
bin/magento cron:run
-
Enter the
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.
-
Run the SQL query
SELECT * from cron_schedule WHERE job_code like '%custom%'
as follows:-
Enter
mysql -u magento -p
-
At the
mysql>
prompt, enteruse magento;
-
Enter
SELECT * from cron_schedule WHERE job_code like '%custom%';
The result should be similar to the following:
+-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+ | schedule_id | job_code | status | messages | created_at | scheduled_at | executed_at | finished_at | +-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+ | 3670 | custom_cronjob | success | NULL | 2016-11-02 09:38:03 | 2016-11-02 09:38:00 | 2016-11-02 09:39:03 | 2016-11-02 09:39:03 | | 3715 | custom_cronjob | success | NULL | 2016-11-02 09:53:03 | 2016-11-02 09:53:00 | 2016-11-02 09:54:04 | 2016-11-02 09:54:04 | | 3758 | custom_cronjob | success | NULL | 2016-11-02 10:09:03 | 2016-11-02 10:09:00 | 2016-11-02 10:10:03 | 2016-11-02 10:10:03 | | 3797 | custom_cronjob | success | NULL | 2016-11-02 10:24:03 | 2016-11-02 10:24:00 | 2016-11-02 10:25:03 | 2016-11-02 10:25:03 | +-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+
-
-
(Optional) Verify that messages are written to Commerce’s system log:
cat /var/www/html/magento2/var/log/system.log
You should see one or more entries like the following:
[2016-11-02 22:17:03] main.INFO: Cron Works [] []
These messages come from the
execute
method inTest.php
:public function execute() { $this->logger->info('Cron Works');
If the SQL command and system log contain no entries, run the magento cron:run
command a few more times and wait. It can take some time for the database to update.
Step 7 (optional): Set up a custom cron group
This step shows how to optionally set up a custom cron group. You should set up a custom cron group if you want your custom cron job to run on a different schedule than other cron jobs (typically, once per minute) or if you want several custom cron jobs to run with different settings.
To set up a custom cron group:
-
Open
crontab.xml
in a text editor. -
Change
<group id="default">
to<group id="custom_crongroup">
-
Exit the text editor.
-
Create
/var/www/html/magento2/app/code/Magento/SampleMinimal/etc/cron_groups.xml
with the following contents:<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd"> <group id="custom_crongroup"> <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>
For a description of what the options mean, see Customizing crons reference.
Step 8: Verify your custom cron group
This optional step shows how to verify your custom cron group using the Admin.
To verify your custom cron group:
-
Run Commerce cron jobs for your custom group:
php /var/www/html/magento2/bin/magento cron:run --group="custom_crongroup"
Run the command at least twice.
-
Clean the cache:
php /var/www/html/magento2/bin/magento cache:clean
-
Log in to the Admin as an administrator.
-
Click Stores > Settings > Configuration > Advanced > System.
-
In the right pane, expand Cron.
Your cron group displays as follows: