{"id":6697,"date":"2014-06-24T14:46:01","date_gmt":"2014-06-24T11:46:01","guid":{"rendered":"http:\/\/blog.belvg.com\/?p=6697"},"modified":"2019-03-28T09:17:47","modified_gmt":"2019-03-28T07:17:47","slug":"using-magentos-shell-the-right-way","status":"publish","type":"post","link":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html","title":{"rendered":"Using Magento\u2019s Shell the &#8220;Right&#8221; Way"},"content":{"rendered":"<p>Every now and then any developer faces the necessity to use shell scripts in Magento. Most often they use standard methods:<\/p>\n<ol>\n<li>Create a script file (for example, call it testscript.php and add it to app\/code\/community\/Mynamespace\/Mymodule\/Model\/) with the following content:<!--more-->\n<pre class=\"lang:php decode:true\">    &lt;?php\r\n    try{    \r\n        chdir (dirname(__FILE__));\r\n        chdir ('..\/..\/..\/..\/..\/');\r\n        require_once('Mage.php');\r\n        Mage::app();\r\n    } catch (Exception $e) {\r\n        print_r('Caught exception: '.$e-&gt;getMessage());\r\n    }\r\n\r\n    \/* code *\/\r\n\r\n    ?&gt;<\/pre>\n<\/li>\n<li>It is executed from the console with the following command or by using shell_exec():\n<pre class=\"lang:php decode:true \"> nohup php &lt;magento_root&gt;\/app\/code\/community\/Mynamespace\/Mymodule\/Shell\/testcript.php  &lt;magento_root&gt;\/shell  &gt; \/dev\/null &amp; echo $!<\/pre>\n<\/li>\n<\/ol>\n<p>Of course this is a working way and you can use it. Nevertheless, Magento already has a class to work with shell scripts &#8211; M&#1072;ge_Shell_Abstr&#1072;&#1089;t. Precisely this one is used as a parent class for all standard Magento shell scripts (it is located in &lt;m&#1072;gent&#1086;_r&#1086;&#1086;t&gt; \/shell). Thus we must simply inherit M&#1072;ge_Shell_Abstr&#1072;&#1089;t in our script.<\/p>\n<p>Advantages of this way are quite evident:<\/p>\n<ul>\n<li>First of all, this is the correct &#8216;M&#1072;gent&#1086; way&#8217;<\/li>\n<li>Automatic connection of Mage.php (via _&#1089;&#1086;nstru&#1089;t())<\/li>\n<li>The presence in M&#1072;ge_Shell_Abstr&#1072;&#1089;t of ready-made useful methods (_&#1072;pplyPhpV&#1072;ri&#1072;bles( ),_p&#1072;rseArgs( ),getArg() etc)<\/li>\n<\/ul>\n<p>All you need to do when creating&#160; a new script is the following:<\/p>\n<ul>\n<li>Add the function require_&#1086;n&#1089;e( &lt;p&#1072;th_t&#1086;_&#1072;bstr&#1072;&#1089;t.php&gt;)<\/li>\n<li>Create a new class and inherit it from M&#1072;ge_Shell_Abstr&#1072;&#1089;t<\/li>\n<li>Be sure to add the method run() into the new class you created (as it is an abstract class in the parent class)<\/li>\n<li>Create an object of the new class and execute the run() method<\/li>\n<\/ul>\n<p>So, this is the results we got with the testscript.php file:<\/p>\n<pre class=\"lang:php decode:true\">    &lt;?php\r\n    require_once $argv[1] . '\/abstract.php';\r\n    class Mynamespace_Mymodule_Shell_Testscript extends Mage_Shell_Abstract\r\n    {\r\n\r\n        public function run()\r\n        {\r\n            \/* your code *\/\r\n        }\r\n\r\n    }\r\n    $obj = new Mynamespace_Mymodule_Shell_Testscript(); \r\n    $obj-&gt;run();\r\n    ?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"Every now and then any developer faces the necessity to use shell scripts in Magento. &#8230;","protected":false},"author":30,"featured_media":8959,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_acf_changed":false,"footnotes":""},"categories":[14],"tags":[3],"class_list":["post-6697","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-magento","tag-magento","post_format-post-format-image"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"Every now and then any developer faces the necessity to use shell scripts in Magento. Most often they use standard methods:\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Andrei Danilchyk\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#article\",\"name\":\"Using Magento\\u2019s Shell the \\\"Right\\\" Way | BelVG Blog\",\"headline\":\"Using Magento\\u2019s Shell the &#8220;Right&#8221; Way\",\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrei-danilchyk#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/06\\\/blog-shell1.jpg\",\"width\":682,\"height\":170},\"datePublished\":\"2014-06-24T14:46:01+02:00\",\"dateModified\":\"2019-03-28T09:17:47+02:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#webpage\"},\"articleSection\":\"Magento, magento, Image\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento#listItem\",\"name\":\"Magento\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento#listItem\",\"position\":2,\"name\":\"Magento\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#listItem\",\"name\":\"Using Magento\\u2019s Shell the &#8220;Right&#8221; Way\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#listItem\",\"position\":3,\"name\":\"Using Magento\\u2019s Shell the &#8220;Right&#8221; Way\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento#listItem\",\"name\":\"Magento\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrei-danilchyk#author\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrei-danilchyk\",\"name\":\"Andrei Danilchyk\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/035c7a3d986ac8d09b60f02a1d47ab8822a92657f10160d588ea28f14db1efb7?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Andrei Danilchyk\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#webpage\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html\",\"name\":\"Using Magento\\u2019s Shell the \\\"Right\\\" Way | BelVG Blog\",\"description\":\"Every now and then any developer faces the necessity to use shell scripts in Magento. Most often they use standard methods:\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrei-danilchyk#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrei-danilchyk#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/06\\\/blog-shell1.jpg\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html\\\/#mainImage\",\"width\":682,\"height\":170},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/using-magentos-shell-the-right-way.html#mainImage\"},\"datePublished\":\"2014-06-24T14:46:01+02:00\",\"dateModified\":\"2019-03-28T09:17:47+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Using Magento\u2019s Shell the \"Right\" Way | BelVG Blog","description":"Every now and then any developer faces the necessity to use shell scripts in Magento. Most often they use standard methods:","canonical_url":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#article","name":"Using Magento\u2019s Shell the \"Right\" Way | BelVG Blog","headline":"Using Magento\u2019s Shell the &#8220;Right&#8221; Way","author":{"@id":"https:\/\/belvg.com\/blog\/author\/andrei-danilchyk#author"},"publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2014\/06\/blog-shell1.jpg","width":682,"height":170},"datePublished":"2014-06-24T14:46:01+02:00","dateModified":"2019-03-28T09:17:47+02:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#webpage"},"isPartOf":{"@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#webpage"},"articleSection":"Magento, magento, Image"},{"@type":"BreadcrumbList","@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/belvg.com\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento#listItem","name":"Magento"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento#listItem","position":2,"name":"Magento","item":"https:\/\/belvg.com\/blog\/category\/magento","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#listItem","name":"Using Magento\u2019s Shell the &#8220;Right&#8221; Way"},"previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#listItem","position":3,"name":"Using Magento\u2019s Shell the &#8220;Right&#8221; Way","previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento#listItem","name":"Magento"}}]},{"@type":"Organization","@id":"https:\/\/belvg.com\/blog\/#organization","name":"BelVG Blog","description":"Magento and Prestashop articles","url":"https:\/\/belvg.com\/blog\/"},{"@type":"Person","@id":"https:\/\/belvg.com\/blog\/author\/andrei-danilchyk#author","url":"https:\/\/belvg.com\/blog\/author\/andrei-danilchyk","name":"Andrei Danilchyk","image":{"@type":"ImageObject","@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/035c7a3d986ac8d09b60f02a1d47ab8822a92657f10160d588ea28f14db1efb7?s=96&d=mm&r=g","width":96,"height":96,"caption":"Andrei Danilchyk"}},{"@type":"WebPage","@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#webpage","url":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html","name":"Using Magento\u2019s Shell the \"Right\" Way | BelVG Blog","description":"Every now and then any developer faces the necessity to use shell scripts in Magento. Most often they use standard methods:","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/belvg.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#breadcrumblist"},"author":{"@id":"https:\/\/belvg.com\/blog\/author\/andrei-danilchyk#author"},"creator":{"@id":"https:\/\/belvg.com\/blog\/author\/andrei-danilchyk#author"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2014\/06\/blog-shell1.jpg","@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html\/#mainImage","width":682,"height":170},"primaryImageOfPage":{"@id":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html#mainImage"},"datePublished":"2014-06-24T14:46:01+02:00","dateModified":"2019-03-28T09:17:47+02:00"},{"@type":"WebSite","@id":"https:\/\/belvg.com\/blog\/#website","url":"https:\/\/belvg.com\/blog\/","name":"BelVG Blog","description":"Magento and Prestashop articles","inLanguage":"en-US","publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"}}]}},"aioseo_meta_data":{"post_id":"6697","title":"Using Magento\u2019s Shell the &quot;Right&quot; Way | #site_title","description":"Every now and then any developer faces the necessity to use shell scripts in Magento. Most often they use standard methods:","keywords":[{"label":"Magento\u2019s Shell","value":"Magento\u2019s Shell"}],"keyphrases":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_custom_url":null,"og_image_custom_fields":null,"og_image_url":null,"og_image_width":null,"og_image_height":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_image_url":null,"twitter_title":null,"twitter_description":null,"schema_type":"default","schema_type_options":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null,"created":"2026-11-03 18:38:01","updated":"2026-11-03 18:38:01"},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/category\/magento\" title=\"Magento\">Magento<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUsing Magento\u2019s Shell the \u201cRight\u201d Way\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/belvg.com\/blog\/"},{"label":"Magento","link":"https:\/\/belvg.com\/blog\/category\/magento"},{"label":"Using Magento\u2019s Shell the &#8220;Right&#8221; Way","link":"https:\/\/belvg.com\/blog\/using-magentos-shell-the-right-way.html"}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/6697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/users\/30"}],"replies":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/comments?post=6697"}],"version-history":[{"count":-2,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/6697\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media\/8959"}],"wp:attachment":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media?parent=6697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/categories?post=6697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/tags?post=6697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}