1

I've set up the cron job for magento. Do I have to set up a separate cron job for reindex? I'm running Magento 1.9.1

2

Hi you can do this by can php script with include mage.php at that script

Step1: create php file and include mage.php at head section of this php file and set store as a admin

require_once "yourmagneto instacedir/www/www/app/Mage.php";
umask( 0 );
Mage :: app( "admin" );

Step2: get Collection of indexes and try to redindex by each index

  try {
        foreach ($indexCollection as $index) {
                    /* @var $process Mage_Index_Model_Process */
                    $process = $indexer->getProcessById($index->getId());
                    if ($process) {
                        $process->reindexEverything();
                    }
                }
                $count = count($processIds);

                   $meassage.= 'Total of %d index(es) have reindexed data'.$count;

            } catch (Mage_Core_Exception $e) {
               $meassage.= $e->getMessage();

         // $this->_getSession()->addError($e->getMessage());
            } 

Step3: add this php file to cron job

Or

you can do this by shell program

php indexer.php --reindex catalog_product_price

format

php indexer.php --reindex indexcode

Default index code:

Process Name -> ID Code

Product Prices -> catalog_product_price

Catalog URL Rewrites -> catalog_url

Product Flat Data -> catalog_product_flat

Category Flat Data -> catalog_category_flat

Category Products -> catalog_category_product

Catalog Search Index -> catalogsearch_stock

Stock Status -> cataloginventory_stock

Tag Aggregation Data -> tag_summary

http://www.clounce.com/magento/magento-reindex-programmatically

2

No need to create a separated cron job, we have in build cron functionality in magento.

Simply you need to set on server.

*/5 * * * * curl -s -o /dev/null

http://www.yoursite.com/absolute/path/to/magento/cron.php

and add reindexing code bottom of cron job(cron.php) class.

// this loops through all indexes and processes those that say they require re indexing

include_once '../app/Mage.php';

    $mageRunCode = isset ( $_SERVER ['MAGE_RUN_CODE'] ) ? $_SERVER ['MAGE_RUN_CODE'] : '';
    $mageRunType = isset ( $_SERVER ['MAGE_RUN_TYPE'] ) ? $_SERVER ['MAGE_RUN_TYPE'] : 'store';

        $app = Mage::app ( $mageRunCode, $mageRunType );
        for($i=3; $i<=9; $i++){
            $process = Mage::getSingleton('index/indexer')->getProcessById($i);
            $state = $process->getStatus();
        //    echo '<pre>'.$process->getData('indexer_code').': '.htmlentities(print_r($process->getStatus(),true)).'</pre>';
            if($process->getStatus() == 'require_reindex'){
                $process->reindexEverything();
            }
        }
2

To make separate & custom cron job for re-indexing follow below code/steps.

  • Add cron job into config.xml as below.
  • <crontab> <jobs> <my_cronscript_custom_re_index> <schedule><cron_expr>30 0 * * *</cron_expr></schedule> <run><model>MODULE_NAME/customreindex::runCustomReIndex</model></run> </my_cronscript_custom_re_index> </jobs> </crontab>
  • 30 0 * * * this cron will run every mid-night
  • Cron model file Customreindex.php
  • class MyPackage_MyModule_Model_Customreindex { public function runCustomReIndex() { Mage::app()->getCacheInstance()->flush(); Mage::app()->cleanCache();
    $indexCollection = Mage::getModel('index/process')->getCollection(); foreach ($indexCollection as $index) { $index->reindexAll(); }
    } }
  • Mage::app()->getCacheInstance()->flush(); and Mage::app()->cleanCache(); are to clear cache. You can avoid it if just need to re-index
1

In short No, you don't need a separate cron task, Magento's internal cron as long as you have at least one cronjob setup for cron.php OR cron.sh then the internal cron will handle any scheduled tasks required.

  • here. On the bottom it says since version 1.4 "reindex all" is left out. What does that mean? The cron job won't reindex data? Should I add it back? – Shawn Jan 6 '15 at 18:54
  • Consult the 1.9 code if you need more specifics as that is a pretty dated article: 2012. – B00MER Jan 6 '15 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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