Symfony2 run console command in background Symfony2 run console command in background symfony symfony

Symfony2 run console command in background


Processes hierarchical

All processes in system have own hierarchical.

As example: we have a Process A, after the launch of which we run Process B. If you kill Process A, then the Process B killed to, because Process B is child of Process A.

You problem

The each request (http) Apache create a new child process for run PHP code and return stdoutput to client (Logic of Nginx + PHPFPM - same). And after create child process (via Symfony/Process library), this process is child of apache or fpm process. After complete request (return response to apache or nginx), the server kill child process (where executed PHP code).

Solutions for you:

  1. Good idea for runs background commands - use nohup (tutorial)
  2. The vary good idea for any applications - use AMQP protocol between processes. (tutorial via RabbitMQ)

P.S.

In my projects, for runs background tasks, i use RabbitMQ.


Let me extend @CerapuStefan 's solution

exec('bash -c "exec nohup setsid '.$this->get('kernel')->getRootDir().'/console update:stock --env=dev > /dev/null 2>&1 &"');

nohup is important


you can set output to new NullOutput.

   $output = new NullOutput();   $application->run($input, $output);

But the best thing is to use RabbitMqueue