Writing a custom magento shellscript

Magento provides the command line shell scripts which will reduce the tedious admin operations. These operation when they performed using browser may get browser timeout when the database or catalog is huge. The Magento Root directory contains a folder named “shell” which contains some default shell scripts for performing the below operations through command Line.

1. indexer.php – To Perform index management operation. (reindex)

2. log.php – To manage visitor logs

3. compiler.php – Used to perform Magento compiler operation

4. abstract.php – Base class for all the shell scripts. In our example scenario we will write a new shell script to update the inventory data with a CSV file and execute this operation in command line interface.

Steps to write a Custom magento shell script

Step 1: Lets create a sample Inventory.csv file which contains the Product SKU and Qty and place it inside “Shell” directory”

Step 2: Lets create our custom shell script named “Mydons_Shell_Inventory” which extends the Abstract class as shown below.

In the Above script the “run” method contains the business logic. The script read the CSV file and loads the product model based on sku and update the qty field. Step 3: To run the custom shell script open the SSH or telnet and connect to your site root directory.

Step 4: Navigate to shell directory and execute the below commands php -f inventory.php”