Magento2 Configure custom Cron jobs

Using Magento2 Cron Jobs can run scheduled tasks like

  • reindexing
  • newsletters
  • generating emails,
  • sitemaps..etc.
  • In Magento2, it’s very easy to configure and saving in the database tables to run tasks at scheduled times.

Follow the below steps to create cron jobs.

Create crontab.xml file under /etc folder.

This file will defined the schedule and the cron class.


<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <job name="magechallenge__order_update" instance="MageChallenge\SetupData\Cron\OrderUpdate" method="execute">
            <schedule>*/30 * * * *</schedule>

This cron will run every 30 minutes.

Define the cron class app/code/MageChallenge/SetupData/Cron/OrderUpdate.php

namespace MageChallenge\SetupData\Cron;

 * Class OrderUpdate
 * @package MageChallenge\SetupData\Cron
class OrderUpdate
     * @var \Magento\Framework\App\State
    private $state;
     * @var \MageChallenge\SetupData\Model\UpdateOrder

     * OrderUpdate constructor.
     * @param \Magento\Framework\App\State $state
    public function __construct(
        \Magento\Framework\App\State $state
    ) {
        $this->state = $state;

    public function execute()
        try {
            // yor can write you own code here
        } catch (\Exception $e) {

Run the cron job

You can run the Magento2 cron jobs using the below command.

php bin/magento cron:run.

Can check this cron on cron_schedule table

Thank you. Have a nice day. 🙂

Latest posts by Chathura Janaka Liyanage (see all)

5 thoughts on “Magento2 Configure custom Cron jobs

  1. A lot of thanks for all your efforts on this site. Gloria enjoys engaging in investigation and it’s easy to understand why. Almost all know all concerning the dynamic means you deliver great items on the web blog and as well encourage contribution from other people on that concern plus our daughter is actually being taught a lot. Have fun with the remaining portion of the new year. You’re the one conducting a glorious job.

Leave a Reply