Most of you already know that by default Magento has a few shell scripts available in {MAGENTO/shell/} we can use from command line. If someone tries to perform the script function from the frontend, it can cause a timeout and create issues in the server resources. Therefore, it is highly recommended that it should be used from CLI which can increase productivity and can save time. Furthermore, we can also schedule php shell scripts via CRON which can save a lot of time.

Next, I will be talking about Shell directory. Every file available under shell directory has its own function which is explained below:
Abstract.php
Abstract.php contains Mage_Shell_Abstract class, which is used by other shell scripts available in shell directory. Custom shell scripts can be created by using few methods that are available in this file; methods like parseArgs(), _applyPhpVariables(), etc. This is mainly for advanced users. You can skip this and move to other shell scripts.
Compiler.php
Magento Compilation was introduced in Magento Community Edition 1.4.2. This feature combines class files that are scattered in app/code/local/, app/code/community/, app/code/core/, and lib/ into a single location in /includes/src/. (The include directory must be writable to make this work.)
Compilation can reduce lookup time of these classes as all of them will be available in the single location. Installation, upgradation, and removal of any plugin require this feature to be disabled. Compilation can be disabled from admin, but if you want to disable it from CLI (or you forgot to disable it before installing the extension and you are unable to access admin panel), then given below is the usage of compiler.php:
@> php compiler.php --help
Usage:php -f compiler.php -- [options]
stateShows Compilation State
compileRuns Compilation Process
clearDisables Compiler include path and removes compiled files
enableEnables Compiler include path
disableDisables Compiler include path
helpProvides help
Example usage:php compiler.php --disable
Indexer.php
Magento indexes the products data in separate database tables to provide fast access on request, but this requires re-indexing if any changes are made in products, prices, etc. This is done to ensure that the changes are reflected on the frontend. This helps in performance when you have a large number of products, but it can cause issues with server resources and timeouts when re-indexing from admin. Indexer.php can help re-indexing all the indexers from CLI. We can also set CRON of indexer.php and can execute it on daily, weekly, or monthly basis. Below is the usage of indexer.php:
@> php indexer.php helpUsage:
php -f indexer.php -- [options]
--status <indexer>Shows Indexer(s) status
--mode <indexer>Show Indexer(s) index mode
--mode-realtime <indexer>Set index mode type “Update on Save”
--mode-manual <indexer>Set index mode type “Manual Update”
--reindex <indexer>Reindex data
infoShow allowed indexers
reindexallReindex data by all indexers
helpProvides help
<indexer>Comma separated indexer codes or value “all” for all indexers
Example usage:php indexer.php reindexall
With large database, it is not recommended to re-index all indexers at the same time. Individual indexers can be identified with following command.
@> php indexer.php info
catalog_product_attributeProduct Attributes
catalog_product_priceProduct Prices
catalog_urlCatalog URL Rewrites
catalog_category_productCategory Products
catalogsearch_fulltextCatalog Search Index
cataloginventory_stockStock Status
tag_summaryTag Aggregation Data
After identifying indexers, you can trigger indexing on them with following command:
@> php indexer.php --reindex catalog_product_attribute
Log.php
Magento maintains logs of visitors, URL, etc., in its database. Following tables are used for maintaining logs:
log_customer
log_visitor
log_visitor_info
log_url
log_url_info
log_quote
report_viewed_product_index
report_compared_product_index
report_event
catalog_compare_item
The size of these tables increases as the time passes by. Therefore, these tables require cleaning from time to time. Log cleaning can be scheduled from admin, but we can use CLI mode to manually clean log tables or for scheduling via CRON. Below is the usage of log.php:
Usage:
php -f log.php -- [options]
php -f log.php -- clean --days 1
cleanCleans logs
--days <days>Saves logs, days. (Minimum 1 day, if defined – ignoring system value)
statusDisplays statistics per log tables
helpProvides help
Example usage:php log.php -- clean
If you want to retain a 3-day log:
Example usage:php log.php -- clean --days 3
Well, that’s about it. I hope I have added to your knowledge and hope that this post helped you in completing your tasks in a more efficient and effective manner, especially when it comes to Magento compilation, indexing, and log cleaning.
If you have any feedback or suggestions about php shell script, then do let me know by using the comments section below.
Get super fast Magento cloud servers on Cloudways
The beauty of Cloudways Click&Go platform is that it allows you the convenience of doing almost everything with your Magento environment. Using SSH terminal, you can run these scripts on your Magento cloud server. If you are not using Click&Go platform, start a free trial from the banner given below and check out everything that we have to offer.
Abdur Rahman
Abdur Rahman is the Magento whizz at Cloudways. He is growth ambitious, and aims to learn & share information about Ecommerce & Magento Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]