0

My cron file code as follows.crontab.xml

<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="My\Catalog\Cron\CheckCatalogMrp" method="execute" name="My_catalog_cron">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

Cron is not running and when i run the command php bin/magento cron:run It is giving output as "Ran Jobs by Schedule"

But my magento.cron.log file have this error. How can i resolve this error?

sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected

My execute method as follows.

public function execute()
    {  
       /* $state = $this->_objectManager->get('Magento\Framework\App\State');
        $state->setAreaCode('adminhtml');*/
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();  
       /* $directory = 'PosMrp';
        if(!is_dir(BP.'var/log/'.$directory)) {
            mkdir(BP.'var/log/'.$directory,0777,true);
        } */

        $log_file = 'return_'.date('Y-m-d').'.log';
        $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/'.$log_file);
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);        
        $today1 = date("m/d/Y H:i:s");
          $logger->info($today1.' - today');

         //$cenvertedTime = date('Y-m-d H:i:s',strtotime('+5 hour +30 minutes',strtotime($today1)));
        $barcodes = array();
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToFilter('sku',array('neq' => 'wallet_product'));
        $collection->setPageSize(10);
        foreach ($collection as $product) {
            $barcodes[] = "".$product->getSku()."";             
        }
        $store_id = 14567;
        $call_url = 'http://myesydata.com';
        $request_url = $call_url.'/online/getMrp'; 

        $data = array(
            'store_id' => $store_id,
            'barcodes' =>  $barcodes
        );

        $post = curl_init();
        curl_setopt($post, CURLOPT_URL, $request_url);
        curl_setopt($post, CURLOPT_POST,TRUE);
        curl_setopt($post, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($post, CURLOPT_RETURNTRANSFER, TRUE);
        $response = curl_exec($post);
        curl_close($post);
        $result = json_decode($response, true); 
        if($result['success'] == 1){
            if($result['data']){
                foreach ($result['data'] as $key => $value) {                   
                    $product_rep =  $objectManager->get('\Magento\Catalog\Model\ProductRepository')->get($key);
                    if($product_rep){
                        if($product_rep->getPrice()  != $value && $value != 0){
                            $product_rep->setPrice($value);
                            if($product_rep->save()){
                                 $logger->info($key.' - Price Updated');
                            }else{
                                $logger->info($key.' - Price Update Failed');
                            }
                        }else{
                            $logger->info($key.' - Product Not Found');

                        }
                    }else{
                        $logger->info($key.' - Product Price Same as Previous----ECOM PRICE----'.$product_rep->getPrice().'POS PRICE---'.$value);
                    }
                }           
            }
        }
        $logger->info('Status'.json_encode($result));
        return $this;
    }  
4
  • execute method can you check that one Apr 29, 2020 at 9:33
  • please upload code of this file CheckCatalogMrp in question
    – Devidas
    Apr 29, 2020 at 9:35
  • added my execute function as well. plz check once
    – Dinesh
    Apr 29, 2020 at 9:47
  • please upload full of code not only execute method
    – Devidas
    Apr 29, 2020 at 9:51

1 Answer 1

0

Check below code

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="my_catalog_cron" instance="My\Catalog\Cron\Checkcatalogmrp" method="execute">
            <schedule>0 1 * * *</schedule>
        </job>                           
    </group>
</config>

My\Catalog\Cron\Checkcatalogmrp.php

<?php

namespace My\Catalog\Cron;

use Magento\Framework\App\ObjectManager;

class Checkcatalogmrp {
    protected $logger;

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

        //Write your code here



    }

}

Your Answer

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

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