Skip to main content

Magento 2 - Shell script or Commands

In magento 2, the shell folder is taken off and instead commands has been introduced. Now, for any shell script, we need to use the commands via php bin/magento commandname. You can find the sample code in the git repo.

Add the files as per the blog post. First of all we need to add a dependency injection, which direct the command file to the desired location.

The di.xml file inside the etc folder will have the info regarding the command class.
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
            <argument name="commands" xsi:type="array">
                <item name="mySampleCommand" xsi:type="object">VendorName\SampleModule\Console\Command\SampleCommand</item>

The class VendorName\SampleModule\Console\Command\SampleCommand, will have the code to execute the command.

namespace VendorName\SampleModule\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class SampleCommand extends Command {

    protected $_limit;
    protected $_mylimit;

    protected function configure() {
                ->setDescription('Update the data using command.')

    protected function execute(InputInterface $input, OutputInterface $output) {
        $output->writeln("Limit = {$this->_limit}");
        $output->writeln("Option Limit = {$this->_mylimit}");

        try {
            //code to execute
            $output->writeln('Updated the command.');
        } catch (LocalizedException $e) {
        } catch (\Exception $e) {
            $output->writeln('Not able to update');

    public function addArguments($input) {
        $this->_limit = intval($input->getArgument("limit"));
        $this->_mylimit = intval($input->getOption("mylimit"));

    public function getInputList() {
        $inputList = [];
        $inputList[] = new InputArgument('limit', InputArgument::OPTIONAL, 'Collection Limit as Argument', 100);
        $inputList[] = new InputOption('mylimit', null, InputOption::VALUE_OPTIONAL, 'Collection Limit as Option', 100);
        return $inputList;


The configure function adds the command name and description. Also, we can enter the input list, which can be entered as parameters. The below command is used to get the info for the command, where "-h" is short form of help. Remove -h and excecute the command.
php bin/magento mysample:update -h


Popular posts from this blog

Magento - Functional Testing

We have checked on how to do unit testing in magento in previous post . That is not enough to test the magento website. If we can testing the magento home page, login page, etc. it would be great. Fortunately, we have a module that deals with this situation.

Magento - Unit Testing

Magento 2 comes with default unit testing in built. But, for magento 1, it is very difficult to perform unit testing. There is an extension EcomDev_PHPUnit, which can help us in performing unit tests in magento 1.