1

I have created a custom module and created the cron in magento 2.2.6

crontab.xml Learning\GreetingMessage\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="index">
<job name="samplecron" instance="Learning\GreetingMessage\Cron\Test" method="execute">
<schedule>*/1 * * * *</schedule>
</job>
</group>
</config>

Test.php Learning\GreetingMessage\Cron\Test.php

<?php
namespace Learning\GreetingMessage\Cron;

class Test {

    protected $_logger;
    protected $_test;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        **\Learning\GreetingMessage\Controller\Index\Text $text**
    ) {
        $this->_logger = $logger;
        $this->_test = $test;
    }

    /**
     * Method executed when cron runs in server
     */
    public function execute() {
        **$this->_test->execute();**
        $this->_logger->debug('Running Cron from Test class');
        return $this;
    }
}

I have controller in Learning\GreetingMessage\Controller\Index\Text.php

It is not working . it says Class Learning\GreetingMessage\Controller\Index\Text does not exist message in cron_schedule table

5
  • You have given instance="Learning\GreetingMessage\Cron\Test" and checking in controller file. You need to create the file at path Learning\GreetingMessage\Cron with name Test.php and create execute method in that file. Also make sure cron is working on your setup. You can execute crons by running command : php bin/magento cron:run Commented Nov 22, 2018 at 13:03
  • @himmat , i already created Test.ph file for cron (That one is pasted in my question). i have Test.php controller also. i already execute php bin/magento cron:run. my cron joab name is also created in "cron_schedule" table. in the table they show message as "Class Learning\GreetingMessage\Controller\Index\Text does not exist". But the controller class is their Commented Nov 22, 2018 at 13:07
  • @himmat if i remove controller call lines from my cron file. it executed with success message Commented Nov 22, 2018 at 13:09
  • May be, basically you don't need controller action to run cron. what you need is Test.php at given path. Please check my answer below.. Commented Nov 22, 2018 at 13:11
  • Check my answer and follow the steps as per your requirements. Commented Nov 22, 2018 at 13:22

2 Answers 2

2

Follow this flow and set file name as per your requirement :

Create a file to run cron :

/var/www/html/Magento2/app/code/Learning/GreetingMessage/Cron/Test.php

<?php

namespace Learning\GreetingMessage\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() {
    // Do your Stuff
    $this->logger->info('cron working successfully');
  }
}

Create crontab.xml

/var/www/html/Magento2/app/code/Learning/GreetingMessage/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="custom_cronjob" instance="Learning\GreetingMessage\Cron\Test" method="execute">
        <schedule>*/1 * * * *</schedule>
    </job>
</group>
</config>

Now, execute command :

php bin/magento cron:run

9
  • this is working fine for me. i have a problem while call controller function inside the cron execute() method Commented Nov 22, 2018 at 13:24
  • I have a controller in "Learning\GreetingMessage\Controller\Index\Text.php" with function execute. i need to call the controller execute function in cron. Commented Nov 22, 2018 at 13:27
  • 1
    change instance in crontab.xml file => instance="Learning\GreetingMessage\Controller\Index\Text" Commented Nov 22, 2018 at 13:28
  • 1
    if you add controller path in construct then please remove it. Commented Nov 22, 2018 at 14:28
  • 1
    Yes , now it is working thank you for sharing knowledge. Commented Nov 23, 2018 at 5:01
0

Give a try to below code:

<?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="samplecron" instance="Learning\GreetingMessage\Cron\Test" method="execute">
<schedule>*/1 * * * *</schedule>
</job>
</group>
</config>

Create Test.php file at below path: Learning\GreetingMessage\Cron with

put below code in Test.php

<?php 
namespace Learning\GreetingMessage\Cron;

class Test {

    protected $_logger;

    public function __construct(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_logger = $logger;
    }

    /**
     * Method executed when cron runs in server
     */
    public function execute() {
        $this->_logger->debug('Running Cron from Test class');
        return $this;
    }
}
2
  • i already did this code in "Learning\GreetingMessage\Cron" path. I use same name for both controller and cron instance . Commented Nov 22, 2018 at 13:15
  • I edited my question please look Commented Nov 22, 2018 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.