Question

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

Was it helpful?

Solution

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

OTHER TIPS

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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top