title: Symfony2 run console command in background
body: <p>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).</p>
  
  <p>Normally is executed like this:</p>
  
  <pre><code>$application = new Application($kernel);
  $application-&gt;setAutoExit(true);
  
  // AppBundle/Command/UpdateStockCommand.php
        $input = new ArrayInput(array(
            'command' =&gt; 'update:stock',
          ));
  
  $output = new NullOutput();
  $application-&gt;run($input, $output);
  </code></pre>
  
  <p>But running like this the user will have to wait for the task to be finished which can take several minutes.</p>
  
  <p>A solution is:</p>
  
  <pre><code>$kernel = $this-&gt;get('kernel');
  $process = new \Symfony\Component\Process\Process('nohup php '. $kernel-&gt;getRootDir() .'/console update:stock --env='. $kernel-&gt;getEnvironment() .' &gt; /dev/null 2&gt;&amp;1 &amp;');
  //$process-&gt;start();
  $process-&gt;run();
  </code></pre>
  
  <p><strong>No errors given, the controller renders the output, but the task is not executed.</strong></p>
  
  <p>Another solution is:</p>
  
  <pre><code>exec('/usr/bin/php '.$this-&gt;get('kernel')-&gt;getRootDir().'/console update:stock --env=dev &gt; /dev/null 2&gt;&amp;1 &amp;');
  </code></pre>
  
  <p>found here <a href="https://stackoverflow.com/questions/26488386/symfony2-process-lunching-a-symfony2-command">Symfony2 - process launching a symfony2 command</a>
  <strong>but doesn't work on my example.</strong> </p>