0

I want to create a cron job which update product price which are disabled and the cron run at a specific time 1 time in a day.

I have created a custom basic module from taking reference from https://www.mageplaza.com/devdocs/magento-2-create-cron-job/

My crontab.xml (path : app/code/Product/Cron/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 instance="Product\Cron\Cron\Test" method="execute" name="mageplaza_helloworld_cron">
        <schedule>*/1 * * * *</schedule>
    </job>
</group>
</config>

my test.php is

<?php

namespace Cron\Run\Cron; (path : app/code/Product/Cron/Cron/Test.php )

class Test
{

public function execute()
{

    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/cron.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info(__METHOD__);

    return $this;

}
}

But it does't show me message in cron.log file. What am i missing any help is appreciated.

1 Answer 1

1

Add This Files to your Custom Module and first check your module is enable or not. Your Module must be enable.

Create a class to run cron

app/code/VendoreName/ModuleName/Cron

Test.php

<?php
namespace VendoreName\ModuleName\Cron;

use Psr\Log\LoggerInterface;

class Test {
    protected $logger;

    public function __construct(LoggerInterface $logger) {
        $this->logger = $logger;
    }

    public function execute() {
        $this->logger->info('Cron Works');
    }
}

Create crontab.xml

app/code/VendoreName/ModuleName/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="VendoreName\ModuleName\Cron\Test" method="execute">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

Compile and cache clean

bin/magento s:up
bin/magento setup:di:compile
bin/magento cache:clean

Verify the cron job

This step shows how to verify the custom cron job successfully using a SQL query on the cron_schedule database table.

  1. Run Magento cron jobs:
bin/magento cron:run
  1. Enter the magento cron:run command two or three times.

The first time you enter the command, it queues jobs; subsequently, the cron jobs are run. You must enter the command at least twice.

  1. Run the SQL query SELECT * from cron_schedule WHERE job_code like '%custom%'
  1. Verify messages are written to Magento’s system log:
var/www/html/magento_root/var/log/system.log

Click Here for more

I Hope This Helps You.

6
  • Thanks for the response, Yes it is absolutely correct i have done that will you please help me how would i want to update product price using cron job and it if i want to print some data where would i see that @Msquare
    – Prits
    Commented Aug 20, 2020 at 12:58
  • for update product price follow this link magento.stackexchange.com/a/216111/82670
    – Msquare
    Commented Aug 20, 2020 at 13:01
  • you can print in this $this->logger->info($var);
    – Msquare
    Commented Aug 20, 2020 at 13:02
  • It gives me an error on doing $this->logger->info($var) that is Recoverable Error: Object of class Magento\Catalog\Model\ResourceModel\Product\Action\Interceptor could not be converted to string @Msquare
    – Prits
    Commented Aug 20, 2020 at 14:37
  • 1
    I suggest you to create one blank controller and try your logic there and after put your logic here. Because you can not print array here. Or any other object not check here. And it's time consuming to check log.
    – Msquare
    Commented Aug 20, 2020 at 15:43

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.