I have set cron in index group its working fine but every time i am running cron:run command.


<?xml version="1.0"?>
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
<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="vaibhav_stock_update" instance="Vaibhav\Sortby\Controller\Index\Stock" method="execute">
            <schedule>* * * * *</schedule>

Php file.

namespace Vaibhav\Sortby\Controller\Index;

use Magento\Framework\App\Filesystem\DirectoryList;

class Stock  extends \Magento\Framework\App\Action\Action {

    protected $_product;

     * @var Magento\CatalogInventory\Api\StockStateInterface 
    protected $_stockStateInterface;

     * @var Magento\CatalogInventory\Api\StockRegistryInterface 
    protected $_stockRegistry;

    protected $_logFactory;

    public function __construct(\Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Model\Order $order,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\App\ResourceConnection $resourceConnection,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\Product $product,
        \Magento\CatalogInventory\Api\StockStateInterface $stockStateInterface,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
        \Vaibhav\Sortby\Model\LogFactory $logFactory
        ) {
        $this->order = $order;
        $this->storeManager = $storeManager;
        $this->scopeConfig = $scopeConfig;
        $this->resourceConnection = $resourceConnection;
        $this->_product = $product;
        $this->_stockStateInterface = $stockStateInterface;
        $this->_stockRegistry = $stockRegistry;
        $this->_logFactory = $logFactory;

   /*  public function updateProductStock($productId,$stockData) {
        $product=$this->_product->load($productId); //load product which you want to update stock
        $stockItem=$this->_stockRegistry->getStockItem($productId); // load stock of that product
        $stockItem->setData('is_in_stock',$stockData['is_in_stock']); //set updated data as your requirement
        $stockItem->setData('qty',$stockData['qty']); //set updated quantity 
        $stockItem->save(); //save stock of item
        $product->save(); //  also save product

    public function execute() {

        $base_url = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
        // lOGGER
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/stock_inventory_cron.log');
        $logger = new \Zend\Log\Logger();

        $logger->info('Start SI drop cron');

        //echo $dateTime = date('Y-m-d H:i:s');
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                 $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
                 $connection = $resource->getConnection();
                 $tableName = $resource->getTableName('taged_values');
                 /*echo $sql1= "Update $tableName Set flag ='y' where tagkey = '10VG2203'";
                 $sql = "SELECT * FROM `taged_values` where trans_update='Y'";
                 $results = $connection->fetchAll($sql); 
                 /*echo '<pre>'; 
                 echo '</pre>';*/
                     foreach ($results as  $result) {
                    //echo "Sku - ----".$tagkey = $result['tagkey'].'<br>';
                    //echo "Qty - ----".$pieces = $result['pieces'].'<br>';
                   $productSku = $result['tagkey'];
                   $qty = $result['pieces'];
                   $stockItems = $objectManager->get('Magento\CatalogInventory\Model\StockRegistry');
                   $stockItem = $stockItems->getStockItemBySku($productSku);
                   /*$stocktaged = $objectManager->get('Vaibhav\Sortby\Model\logFactory');
                   $stocktagedvales = $stocktaged->getTagkey($productSku);
                 /*  echo '<br>';
                  echo $sql1= "Update taged_values Set trans_update ='U' where 'tagkey' = '" . $productSku . "'";
                     echo '</br>';
                   $logger->info('Start SI drop cron');*/
                    //echo "stock update";
                    //echo '<br>';
                     $sql1= "Update taged_values Set trans_update ='U' where tagkey = '" . $productSku . "'";
                     //echo '</br>';

I have set cron for every minutes its working fine when am i running cron:run command. anyone can suggest me what should i do .

  • * * * * * /usr/bin/php7.0 <MAGENTO_ROOT_PATH>/bin/magento cron:run Please add this in your crontab file. – Dhaduk Mitesh Jul 17 '18 at 12:27
  • I have added its not working – Er Sarvesh V Tiwari Jul 17 '18 at 13:05
  • First, remove all schedule from the database. After that clean cache. and run php bin/magento cron:run command once. And check it. – Dhaduk Mitesh Jul 18 '18 at 5:54
  • I did this again again but every time when am i running command than cron is running – Er Sarvesh V Tiwari Jul 18 '18 at 10:25
  • Did you flush cache? – Dhaduk Mitesh Jul 18 '18 at 10:38

If you use Magento >= 2.2.0 you can try to setup crontab using a command:

php bin/magento cron:install

There is more information: https://devdocs.magento.com/guides/v2.2/config-guide/cli/config-cli-subcommands-cron.html#create-the-magento-crontab

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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