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
4 Answers
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();
andMage::app()->cleanCache();
are to clear cache. You can avoid it if just need to re-index
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 the head section of this PHP file and set store as an admin
require_once "yourmagneto instacedir/www/www/app/Mage.php";
umask( 0 );
Mage :: app( "admin" );
Step2: get Collection of indexes and try to reindex 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
No need to create a separate cron job, we have to build cron functionality in Magento.
Simply you need to set on the 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();
}
}
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.
-
-
Consult the 1.9 code if you need more specifics as that is a pretty dated article: 2012.– B00MERCommented Jan 6, 2015 at 19:00