Magento has a class that specifically handles command line scripts. The Magento bootstrap can be handled via the Shell Abstract script inside of your shell/
folder.
<?php
include_once 'abstract.php';
class JohnG_Shell_Cron extends Mage_Shell_Abstract {
public function run() {
$this->_clearindex();
$this->_clearCache();
}
protected function _clearindex() {
$process = Mage::getModel('index/indexer')->getProcessByCode('catalog_product_flat');
$process->reindexAll();
}
protected function _clearCache() {
Mage::app()->getCacheInstance()->clean('block_html');
}
}
Now setup your cron like this:
0 0-23/2 * * * php -f /path/to/your/magento/shell/johngscript.php
step 1 : cd [MAGENTO_ROOT]/shell/
You should replace [MAGENTO_ROOT] with your own absolute path to Magento root folder. For example /home/Thief/public_html/ Then list all files to see what we have here. Just enter simple command:
step 2 : ls -l
If you can find something like ‘indexer.php‘, that’s what we need and we can go on. To see the status of all indexes, you can execute the command
php indexer.php --status
step 3 : php indexer.php --reindex catalog_product_price
Here catalog_product_price is the index type that you want to rebuild. You can use next arguments for different types of indexes:
catalog_product_attribute Product Attributes
catalog_product_price Product Prices
catalog_url Catalog Url Rewrites
catalog_product_flat Product Flat Data
catalog_category_flat Category Flat Data
catalog_category_product Category Products
catalogsearch_fulltext Catalog Search Index
cataloginventory_stock Stock status
For all reindexall : php indexer.php --reindexall
Background reindex process Sometimes you may want to run this process in background so you can turn off your computer or just have a couple of beers. In this case, you can use a command line tool, called ‘nohup’. It allows you to run almost any CLI command in background independently from your current session. If you want to reindex something in background, your command should look like this:
nohup php indexer.php --reindex catalog_product_price &
For more information refer this link
Best Answer
indexer:reindex
CMD:
Using DI
or by using object manager
Cache flush/Clean:
CMD:
Using DI:
Using ObjectManager: