A Look On Basic PHP Shell Scripts For Magento

by Syed Maad Jahangir  May 5, 2014

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.

Php Shell Scripts

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]
state Shows Compilation State
compile Runs Compilation Process
clear Disables Compiler include path and removes compiled files
enable Enables Compiler include path
disable Disables Compiler include path
help Provides 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 help

Usage: 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
info Show allowed indexers
reindexall Reindex data by all indexers
help Provides 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_attribute Product Attributes
catalog_product_price Product Prices
catalog_url Catalog URL Rewrites
catalog_category_product Category Products
catalogsearch_fulltext Catalog Search Index
cataloginventory_stock Stock Status
tag_summary Tag 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
clean Cleans logs
--days <days> Saves logs, days. (Minimum 1 day, if defined – ignoring system value)
status Displays statistics per log tables
help Provides 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.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Syed Maad Jahangir

Syed Maad Jahangir worked as Senior System Engineer at Cloudways.

Stay Connected:

You Might Also Like...

  • Do I need to set up the cron job in the application console for the log cleaning to work periodically?

  • Syed Maad Jahangir

    Yes, if you want to schedule log cleaning then you have to set up cron job from console.