Attention

TYPO3 v9 has reached its end-of-life September 30th, 2021 and is not maintained by the community anymore. Looking for a stable version? Use the version switch on the top left.

You can order Extended Long Term Support (ELTS) here: TYPO3 ELTS.

Symfony Console Commands (cli)

It is possible to run some TYPO3 CMS scripts from the command line. This makes it possible - for example - to set up cronjobs.

TYPO3 uses Symfony commands to provide an easy to use, well-documented API for writing CLI (command line interface) commands.

These commands can also be run from the TYPO3 scheduler.

New in version 8: TYPO3 supports Symfony Console commands natively since TYPO3 v8.

Extbase Command Controllers are deprecated since v9.4.

Creating a new Symfony Command in Your Extension

  1. Add Configuration/Commands.php to your extension

    TYPO3 looks in this file for configured commands. It should return a simple array with the command name and class.

    For example to add a command named yourext:dothings:

    return [
        'yourext:dothings' => [
            'class' => \Vendor\Extension\Command\DoThingsCommand::class,
        ],
    ];
    
  2. Create the corresponding class file: Classes/Command/DoThingsCommand.php

    Symfony commands should extend the class \Symfony\Component\Console\Command\Command.

    The command should implement at least a configure() and an execute() method.

    configure()

    As the name would suggest allows to configure the command. Allows to add a description or a help text, or mandatory and optional arguments and parameters defined.

    execute()

    Contains the logic when executing the command.

See also

A detailed description and an example can be found in the Symfony Command Documentation.

Command Class

Example taken from ListSysLogCommand in the core and simplified:

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

class DoThingsCommand extends Command
{
    /**
     * Configure the command by defining the name, options and arguments
     */
    protected function configure()
    {
        $this->setDescription('Show entries from the sys_log database table of the last 24 hours.');
        $this->setHelp('Prints a list of recent sys_log entries.' . LF . 'If you want to get more detailed information, use the --verbose option.');
    }

    /**
     * Executes the command for showing sys_log entries
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);
        $io->title($this->getDescription());

        // ...
        $io->writeln('Write something');
        return 0;
    }
}

Return value

Changed in version 9: It is now recommended to return a value in execute(). In TYPO3 version 10 (and symfony/console version 5), using execute() without return will result in an exception. In TYPO3 9, it is recommended to return a value, but the constants Command::SUCCESS or Command::FAILURE are not available (in symfony/console version 4), so you can just return 0 (SUCCESS) or 1 (FAILURE).

The return type is int.

Passing Arguments

\TYPO3\CMS\Install\Command\UpgradeWizardRunCommand:

/**
 * Configure the command by defining the name, options and arguments
 */
protected function configure()
{
    $this->setDescription('Run upgrade wizard. Without arguments all available wizards will be run.')
        ->addArgument(
            'wizardName',
            InputArgument::OPTIONAL
        )->setHelp(
            'This command allows running upgrade wizards on CLI. To run a single wizard add the ' .
            'identifier of the wizard as argument. The identifier of the wizard is the name it is ' .
            'registered with in ext_localconf.'
        );
}

This command takes one optional argument wizardName, which can be passed on the command line:

vendor/bin/typo3 upgrade:run [wizardName]

Deactivating the Command in Scheduler

New in version 9.4: ext_core:Changelog/9.4/Feature-85991-ExcludeSymfonyCommandsFromScheduler

By default, the command can be used in the scheduler too. You can deactivate this by setting schedulable to false in Configuration/Commands.php:

return [
    'yourext:dothings' => [
        'class' => \Vendor\Extension\Command\DoThingsCommand::class,
        'schedulable' => false,
    ],
];

Initialize Backend User

If anything related to DataHandler and backend permission handling is necessary, you should call this initialization method once in your execute() function:

Bootstrap::initializeBackendAuthentication();

Running the Command From the Command Line

The above example can be run via command line:

vendor/bin/typo3 yourext:dothings

Show help for the command:

vendor/bin/typo3 help yourext:dothings

Tip

If you installed TYPO3 without Composer, the path for the executable is typo3/sysext/core/bin/typo3.

Running the Command From the Scheduler

New in version 9.0: Running Symfony Console Commands via the scheduler is possible since TYPO3 v9.0.

The Deactivating the Command in Scheduler option is available since v9.4.

By default, it is possible to run the command from the TYPO3 scheduler as well. To do this, select the task Execute console commands followed by your command in the Schedulable Command field.

Note

You need to save and reopen the task to define command arguments.

In order to prevent commands from being set up as Scheduler tasks, see Deactivating the Command in Scheduler.

More information