× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to Create a Shell Script in Magento 1.x

Ryan Street
Dec 01, 2015
<p>First, create a file inside the shell/ directory in your Magento application:</p> <p>myscript.php</p> <p>Next, open the file and include the 'abstract.php' at the top. </p> <pre><code class="language-php">&lt;?php require_once 'abstract.php';</code></pre> <p>Next, create your class and extend the <code>Mage_Shell_Abstract</code> class.  </p> <pre><code class="language-php">&lt;?php require_once 'abstract.php'; class Coolryan_Shell_Myscript extends Mage_Shell_Abstract { }</code></pre> <p>This abstract class requires a <code>run()</code> method to be defined, so let's do that now. </p> <pre><code class="language-php">&lt;?php require_once 'abstract.php'; class Coolryan_Shell_Myscript extends Mage_Shell_Abstract { public function run() { // here is where your customization will go. } }</code></pre> <p>Finally, instantiate the class at the bottom, and call the <code>run()</code> method:</p> <pre><code class="language-php">&lt;?php require_once 'abstract.php'; class Coolryan_Shell_Myscript extends Mage_Shell_Abstract { public function run() { // here is where your customization will go. } } $shell = new Coolryan_Shell_Myscript(); $shell-&gt;run();</code></pre> <p>Now, whenever you need to run your script, (via cron or just via the shell), you can call it like this:</p> <pre><code class="language-bash">php shell/myscript.php</code></pre> <p>The abstract class takes care of bootstrapping Magento, so you only have to write the code you need to create your script.  Enjoy!</p>
comments powered by Disqus