Skip to main content
Blog General

How To Set and Configure Custom Cron Jobs In Magento 2?

Kavitha M
July 11, 2017 |
EDI

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 DCKAPTestCronTest.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.logYou 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.

[clickandtweet handle=”@DCKAP” hashtag=”” related=”” layout=”” position=””]Thanks for sharing How To Set and Configure Custom Cron Jobs In Magento 2? #magento2 #realmagento[/clickandtweet]

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 to

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

Are you in to Magento eCommerce development? You must learn how to awt and configure cron jobs in Magento 2.

Kavitha M

Kavitha is a Certified Magento Developer Plus Software Developer at DCKAP. A typical Gen Z, she has a great amount of drive and works hard . For her , coding is as much fun as it is work. Loves chess and traveling.

More posts by Kavitha M