-1

Is it possible to execute some background command and without waiting for it completes, return response?(Symfony2) example:user submit form and gets response, but in background executes some external calculations.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Yes, it's possible, but there are several different techniques. Please provide more information about what you're trying to do, so you will receive the appropriate answer. – rjdown May 02 '15 at 16:08
  • Search for "PHP background process" for several approaches. – halfer May 02 '15 at 16:10
  • 1
    possible duplicate of [php execute a background process](http://stackoverflow.com/questions/45953/php-execute-a-background-process) – halfer May 02 '15 at 16:10

2 Answers2

0

Just refer this documentation

You can run the command in the background by adding a "&" at the end of it as:

exec('Your Command &');

Usually you would put the commands in a shell script and call that from exec, however the above trick will help where the sequence is dynamically generated from php.

NOTE:- If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

So you can redirect the stdout of the command to a file, if you want to see it later or to /dev/null if you want to discard it as:

exec('Command > /dev/null &');

For a sequence of commands, enclose them within parentheses then append the & symbol but be sure to redirect stdout, stderr somewhere otherwise your script will hang waiting e.g.:

<?php

exec('( sleep 10; echo "finished" | mail ian@example.com ) &> /dev/null &');

?>

Thanks!

SantanuMajumdar
  • 886
  • 1
  • 5
  • 20
0

The most correct way in my opinion is to use RabbitMQ. You can send message to Rabbit and with an cron job execute needed command. Something like that: http://cases.azoft.com/using-rabbitmq-in-symfony2-projects/