How to setup Magento 2 cron job

Here are the following steps help you how to setup cron job in Magento 2:

Step 1: Form file etc/crontab.xml

Step 2: Navigate to ssh console and run the command: php bin/Magento cron:run

Hi friends, as you know, cron is a linux service which scheduled a command or script on your server to run automatically at a specified time and date.

In Magento 2, it supports cron-work, so some php script can be used to run it automatically. Here, I will exhibit you how to setup the Magento 2 cron job.

Step 1:

  • Firstly, you want to create file etc/crontab.xml:
<?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="Webnexs_cron" instance="Webnexs\Training\Cron\TrainingCron" method="execute">

            <schedule>* * * * *</schedule>

        </job>

    </group>

</config>

The schedule is the time of Cron running. In this example, it is operated every minute

* * * * * *

| | | | | |

| | | | | +-- Year              (range: 1900-3000)

| | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)

| | | +------ Month of the Year (range: 1-12)

| | +-------- Day of the Month  (range: 1-31)

| +---------- Hour              (range: 0-23)

+------------ Minute            (range: 0-59)
  • For example, the class will have the script you want to run. he So you must form the file Webnexs\Training\Cron\TrainingCron.php and note the source code in execute() method:
<?php

namespace Webnexs\Training\Cron;

class TrainingCron

{

    public function execute()

    {

        //Todo

    }

}

Step 2:

  • Navigate to ssh console and run the command:

Php bin/Magento cron:run

  • If you require automating the process, please include the command to /etc/crontab. For example:

* * * * * www-data PHP /home/Eden/public_html/magento2/bin/Magento Cron:run

Notes:

  • ****: It is the time the script autorun (Every minute)
  • www-data: It is the user will run this script
  • php/home/eden/public­_html/magento2/bin/Magento cron: run the command

Last Update: April 5, 2018  

February 28, 2018   346   Nandini R    Magento2    
Total 2 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn