How to run a symfony2 custom command in the background

Symfony2 allows developers to create their own command line commands. They can be executed from the command line, but also from the controller. According to the official Symfony2 documentation, this can be done as follows:

protected function execute(InputInterface $input, OutputInterface $output) { $command = $this->getApplication()->find('demo:greet'); $arguments = array( ... ); $input = new ArrayInput($arguments); $returnCode = $command->run($input, $output); } 

But in this situation, we expect the command to complete execution and return a return code.

How can I, from the controller , execute a command, deploying it to the background, without waiting for the completion of its execution?

In other words, that would be equivalent

 $ nohup php app/console demo:greet & 
+7
source share
2 answers

According to the documentation, I don't think there is such an option: http://api.symfony.com/2.1/Symfony/Component/Console/Application.html

But regarding what you are trying to achieve, I think you should use the process component instead:

 use Symfony\Component\Process\Process; $process = new Process('ls -lsa'); $process->run(function ($type, $buffer) { if ('err' === $type) { echo 'ERR > '.$buffer; } else { echo 'OUT > '.$buffer; } }); 

And as mentioned in the documentation, "if you want some real-time feedback, just pass an anonymous function to the run () method."

http://symfony.com/doc/master/components/process.html

+5
source

From the documentation, it is better to use start () instead of run () if you want to create a background process. Max_time process can kill your process if you create it with run ()

"Instead of using run () to execute the process, you can run () it: run () blocks and waits for the process to complete, start () creates a background process."

+6
source

All Articles