8

Symfony2 enables developers to create their own command-line commands. They can be executed from command line, but also from the controller. According to official Symfony2 documentation, it can be done like that:

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 wait for the command to finish it's execution and return the return code.

How can I, from controller, execute command forking it to background without waiting for it to finish execution?

In other words what would be equivalent of

$ nohup php app/console demo:greet &
luchaninov
  • 6,792
  • 6
  • 60
  • 75
malloc4k
  • 1,742
  • 3
  • 22
  • 22
  • We recently ran into the same issue and solved it using the [RabbitMQBundle](https://github.com/videlalvaro/RabbitMqBundle) – Squazic Dec 10 '12 at 16:42

2 Answers2

7

From the documentation is better use start() instead run() if you want to create a background process. The process_max_time could kill your process if you create it with run()

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

Freenando
  • 405
  • 7
  • 16
  • Can you elaborate on process_max_time? Google does not return any relevant results for this except your post. – gadelat Jul 07 '16 at 08:29
  • @gadelat Probably meant an option in `php.ini`: [`max_execution_time`](http://php.net/manual/en/info.configuration.php#ini.max-execution-time) – Maxim Mandrik Dec 12 '18 at 14:49
6

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 to be able to get some feedback in real-time, just pass an anonymous function to the run() method".

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

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • 1
    Without investigating the details very much, i did this using `$process->start()` instaed of `$process->run()` – malloc4k Dec 13 '12 at 10:29
  • 1
    Seems like run() calls start() and then wait() so in your case you are right, you should use start. [link](https://github.com/symfony/Process/blob/master/Process.php) – cheesemacfly Dec 13 '12 at 15:14