Running Magento from command line

Method 1

<?php
 
require_once dirname(__DIR__) . '/../../../../../app/Mage.php';
$app = Mage::app();
Mage::register('isSecureArea', true);
// your code goes here

or

<?php
 
require_once dirname(__DIR__) . '/../../../../../app/Mage.php';
 
/**
 * Class My Shell Class
 */
class My_Shell_Class {
 
    /**
     * Constructor
     */
    public function __construct() {
        Mage::app();
        Mage::register('isSecureArea', true);
    }
 
    /**
     * Custom function
     */
    public function myFunction() {
	// your code goes here
    }
}
 
$shell = new My_Shell_Class();
$shell->myFunction();

Method 2, using approach similar to indexer.php

<?php
 
require_once dirname(__FILE__) . '/../../../../../../shell/abstract.php';
 
class My_Shell_Class extends Mage_Shell_Abstract
{
 
    /**
     * Run mandatory method 
     *
     */
    public function run()
    {
        if ($this->getArg('status')) {
 
            printf("Your code goes here\n");
 
            printf("Your param1 value".$this->getArg('status')."\n");
 
        } else {
            echo "
Usage:  php -f myshell.php -- [options]
 
     status                   Execute status logic
  --status param1        Execute status logic depending on param1 value
 
  help                 This help
		";
        }
    }
}
 
$shell = new My_Shell_Class();
$shell->run();

Leave a Reply