Here's a better way than hooking into the test system: Use Magento 2's command-line interface.
This does mean you'll have to integrate your sandbox code into an actual module (or create one for the purpose), but you should be doing that anyway.
Once you have your module set up, adding a command is pretty easy. All you need is the class, and DI to register it.
1. {module}/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="greeting_command" xsi:type="object">Magento\CommandExample\Console\Command\GreetingCommand</item>
</argument>
</arguments>
</type>
</config>
2. {module}/Console/Command/GreetingCommand.php
<?php
namespace Magento\CommandExample\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class GreetingCommand
*/
class GreetingCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('example:greeting')
->setDescription('Greeting command');
parent::configure();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Hello world!</info>');
}
}
Example derived from https://github.com/magento/magento2-samples/tree/master/sample-module-command - see there for a complete module incorporating this functionality. There are less-trivial examples included.
By convention, your command class should always be in {module}/Console/Command
, and end with Command.php
.
Once you've added those two bits of code (and flushed Magento cache, etc.), execute your command by name in SSH: php bin/magento example:greeting
.
You can use dependency injection in this context, so you can run any code you want to within execute()
.
This interface is built on Symfony's Console component, so you also have full access to all of that wide range of functionality, including options/arguments, tables, and very easy progress bars.
If you run into any problems setting up your command or options, you can usually run the 'list' command to get better visibility into what's going wrong: php bin/magento list
Enjoy.
cron.php
:$app = $bootstrap->createApplication('Magento\Framework\App\Cron', ['parameters' => ['group::']]);
. Should I create my own app model?