To whom may concern,

As the title said, I'm wondering is there anyway to inject/call/execute Magento default commands in my custom command?

I'm just another guy who living in Terminal world and I'm exhausted while have to press then Enter for those commands all day.

At this moment, I'm forced to write a bash script to shorten it. So I think it is better if I wrote a custom command of Magento 2 to do those jobs. Then I can just simple use bash alias that command in my way.

For example:

I need to execute setup:upgrade, rm -rf var/generation var/di, setup:di:compile in just only one command and few more thing (actually a lot) after setup:di:compile executed successfully.

setup:upgrade is handled by this class \Magento\Setup\Console\Command\UpgradeCommand

setup:di:compile is handle by this one \Magento\Setup\Console\Command\DiCompileCommand

But I'm not sure is it Ok to call execute() method in my custom command?

Anyone have any ideas for this situation?

2 Answers 2


Short answer is yes you can do what you want.

The Magento console commands are built using Symfony (as you probably are aware).

The following is the example taken directly from the documentation.

Ref: http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command

protected function execute(InputInterface $input, OutputInterface $output)
    $command = $this->getApplication()->find('demo:greet');

    $arguments = array(
        'command' => 'demo:greet',
        'name'    => 'Fabien',
        '--yell'  => true,

    $greetInput = new ArrayInput($arguments);
    $returnCode = $command->run($greetInput, $output);

    // ...
protected function execute(InputInterface $input, OutputInterface $output)
    $commandList = array('setup:upgrade', 'cache:clean', 'cache:flush');
    foreach ($commandList as $list) {
        $command = $this->getApplication()->find($list);
        $returnCode = $command->run($input, $output);
        if(!$returnCode) $output->writeln($list . ' successfully finished');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.