I've created a console command for my symfony2 project and I want to execute it from a controller without blocking the controller output (in background).
Normally is executed like this:
$application = new Application($kernel);
$application->setAutoExit(true);
// AppBundle/Command/UpdateStockCommand.php
$input = new ArrayInput(array(
'command' => 'update:stock',
));
$output = new NullOutput();
$application->run($input, $output);
But running like this the user will have to wait for the task to be finished which can take several minutes.
A solution is:
$kernel = $this->get('kernel');
$process = new \Symfony\Component\Process\Process('nohup php '. $kernel->getRootDir() .'/console update:stock --env='. $kernel->getEnvironment() .' > /dev/null 2>&1 &');
//$process->start();
$process->run();
No errors given, the controller renders the output, but the task is not executed.
Another solution is:
exec('/usr/bin/php '.$this->get('kernel')->getRootDir().'/console update:stock --env=dev > /dev/null 2>&1 &');
found here Symfony2 - process launching a symfony2 command but doesn't work on my example.