I'm trying to run a Magento 2 console command from within an adminhtml Controller in a custom module. (The module itself is configured correctly, enabled, and running just fine with no errors.)

I figured I'd give one of the built-in commands a shot to ensure it works properly before attempting my own custom command.

When triggered via a cron job, it runs with no errors:

php-cli /home/[sharedHostingName]/public_html/bin/magento --list > /home/[sharedHostingName]/public_html/var/log/test.log

But if I trigger it from my adminhtml Controller, I get an error part-way through the output:

We're sorry, an error occurred. Try clearing the cache and code generation directories. By default, they are: var/cache, var/di, var/generation, and var/page_cache.

I've tried clearing all four of those directories several times with no luck. I get the same result each time.

What am I doing wrong?

Here's my Controller:


namespace [CompanyName]\Module\Controller\Adminhtml\Test;

class Index extends \Magento\Backend\App\Action

    protected $jsonResultFactory;
    protected $context;
    private $resultData;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory
    ) {

        $this->jsonResultFactory = $jsonResultFactory;
        return parent::__construct($context);


    public function execute()

        $result = $this->jsonResultFactory->create();

        system('php-cli bin/magento --list > /home/[sharedHostingName]/public_html/var/log/test.log');

        $this->resultData["message"] = "test AJAX response";
        return $result;


    protected function _isAllowed()
        return $this->_authorization->isAllowed('ACL RULE HERE');


...And here's the full output written to the log (error at bottom):

Magento CLI version 2.1.2

 command [options] [arguments]

 --help (-h)           Display this help message
 --quiet (-q)          Do not output any message
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 --version (-V)        Display this application version
 --ansi                Force ANSI output
 --no-ansi             Disable ANSI output
 --no-interaction (-n) Do not ask any interactive question

Available commands:
 help                                      Displays help for a command
 list                                      Lists commands
 admin:user:create                         Creates an administrator
 i18n:collect-phrases                      Discovers phrases in the codebase
 i18n:pack                                 Saves language package
 info:adminuri                             Displays the Magento Admin URI
 info:backups:list                         Prints list of available backup files
 info:currency:list                        Displays the list of available currencies
 info:dependencies:show-framework          Shows number of dependencies on Magento framework
 info:dependencies:show-modules            Shows number of dependencies between modules
 info:dependencies:show-modules-circular   Shows number of circular dependencies between modules
 info:language:list                        Displays the list of available language locales
 info:timezone:list                        Displays the list of available timezones
 maintenance:allow-ips                     Sets maintenance mode exempt IPs
 maintenance:disable                       Disables maintenance mode
 maintenance:enable                        Enables maintenance mode
 maintenance:status                        Displays maintenance mode status
 module:disable                            Disables specified modules
 module:enable                             Enables specified modules
 module:status                             Displays status of modules
 module:uninstall                          Uninstalls modules installed by composer
 setup:backup                              Takes backup of Magento Application code base, media and database
 setup:config:set                          Creates or modifies the deployment configuration
 setup:cron:run                            Runs cron job scheduled for setup application
 setup:db-data:upgrade                     Installs and upgrades data in the DB
 setup:db-schema:upgrade                   Installs and upgrades the DB schema
 setup:db:status                           Checks if DB schema or data requires upgrade
 setup:di:compile                          Generates DI configuration and all missing classes that can be auto-generated
 setup:install                             Installs the Magento application
 setup:performance:generate-fixtures       Generates fixtures
 setup:rollback                            Rolls back Magento Application codebase, media and database
 setup:store-config:set                    Installs the store configuration
 setup:uninstall                           Uninstalls the Magento application
 setup:upgrade                             Upgrades the Magento application, DB data, and schema
We're sorry, an error occurred. Try clearing the cache and code generation directories. By default, they are: var/cache, var/di, var/generation, and var/page_cache.

1 Answer 1


This doesn't officially answer my question, but I got the code to work by adjusting my approach.

I changed the flow so that my main logic is in a Model and it's now being called from a Command which is fired from the CLI. This allows me to also call the Model from a Controller so that the website can leverage the same logic as the console.

Both run to completion with no errors now.

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.