I created a console command for my symfony2 project, and I want to execute it from the controller without blocking the controller output (in the background).
Usually done as follows:
$application = new Application($kernel);
$application->setAutoExit(true);
$input = new ArrayInput(array(
'command' => 'update:stock',
));
$output = new NullOutput();
$application->run($input, $output);
But, working this way, the user will have to wait for the completion of the task, which may take several minutes.
Decision:
$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->run();
No errors, the controller displays the output, but the task does not complete.
Another solution:
exec('/usr/bin/php '.$this->get('kernel')->getRootDir().'/console update:stock --env=dev > /dev/null 2>&1 &');
found here Symfony2 - running the symfony2 command
, but does not work on my example.
source
share