Magento uses Cron Jobs to run scheduled tasks, reindexing, generating emails, newsletters, sitemaps and much more. In Magento 2, crons can be configured easily and listed in the database table to process our tasks in scheduled time.

In order to create a custom cron job, please follow the below steps.

Step 1: Create a sample module

If you already have a sample module, you can use it. Make sure the sample module is registered and enabled.

In this example, we are using a sample module with DCKAP as the Vendor name and Test as the module name.

Step 2: Create a class to run cron

Step 2.1 : Create a directory for the class

/var/www/html/Magento2/app/code/DCKAP/Test/Cron/

Step 2.2 : Create a Test.php file in cron folder with the following contents.

This file contains the custom cron code to be executed while the cron runs in the Magento.

In the above code, we created a log. Once the cron is executed, the contents of the ‘execute’ method will be added in the system.log file.

Step 3: Create crontab.xml

crontab.xml sets a schedule to run your custom cron code which is defined in the Test.php file.

create crontab.xml under /var/www/html/Magento2/app/code/DCKAP/Test/etc/

Here crontab.xml runs the “execute” method in DCKAP\Test\Cron\Test.php class once per minute, resulting in a row being added to the cron_schedule table.

Get to know how to set cron format here.

Step 4: Run the cron job

We can run the Magento cron jobs using the below command.

If you want to verify the custom cron job in the terminal through SQL, you can run the above command more than twice and list the table. It will show the success message for crons executed successfully.

SQL query : SELECT * from cron_schedule WHERE job_code like ‘%custom%’

Also you can verify it through /var/www/html/Magento2/var/log/system.log

You should see one or more entries like the following:

[2017-06-20 06:48:40] main.INFO: Cron Works [] []

These messages come from the execute method in Test.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.

Thanks for sharing How To Set and Configure Custom Cron Jobs In Magento 2? #magento2 #realmagento

Step 5: Create custom cron group

To set up a custom cron group:

Step 5.1 : Open crontab.xml in a text editor.

Step 5.2 : Change <group id=”default”> to <group id=”custom_crongroup”>

Step 5.3 : Save the changes

Step 5.4 : Create /var/www/html/Magento2/app/code/DCKAP/Test/etc/cron_groups.xml with the following contents:

Step 6 : Run the custom cron group

Step 6.1 : Run Magento cron jobs for your custom group:

Run the command at least twice.

Step 6.2 : Clean the Magento cache:

To verify, log in to the Magento Admin as an administrator.

Click Stores > Configuration > Advanced > System.

In the right pane, expand Cron. In your “Cron configuration options for group: custom_crongroup” it scheduled with your configurations in cron_groups.xml.

magento 2

Hope this is useful. Please get back to us if you have any queries.

References

Be the first to Know

Subscribe to our blog to get the latest articles directly to your inbox.

READY TO DRIVE SALES?

Request a Quote Today

Request a Quote

Leave a Reply

4 Comments on "How To Set and Configure Custom Cron Jobs In Magento 2?"


Guest
Rahul
2 months 6 days ago

Nice tutorial !

admin
Admin
2 months 5 days ago

Hi Rahul,

Thanks for your comments.
Please keep reading our blogs and posting comments.

Guest
Fayyaz Khattak
1 year 3 months ago

Briefly explained article! It will help newbies to learn and understand the basic structure of configuring and running Magento 2 Cron.

Kavitha M
Author
1 year 3 months ago

Hi Fayyaz,

Thanks for your comments.
Please continue reading our blogs and keep sharing your insights and feedbacks.