Drupal - How to run Drupal console command from code?

Run shell commands via the Process class available in the Symfony framework.

From the Symfony docs:

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

$process = new Process('ls -lsa');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

echo $process->getOutput();

The component takes care of the subtle differences between the different platforms when executing the command.

Replace the ls -lsa command in the Process() constructor with your Drupal console command.


Try using the PHP functions exec() or shell_exec().