2

Possible Duplicate:
Call another PHP script and return control to user before the other script completes

I need to run a PHP script from another PHP script asynchronously.


Script 1

<?php
    echo "Entering main script 1";
    system("php script2.php");
    echo "Exiting main script 1";
?>

Script 2

<?php
    echo "Entering script 2";
    sleep(10);
    echo "Exiting script 2";
?>

In script 1, I use system() method to run script2.php. I dont want script 1 to wait for script 2 to complete its execution. How to make it asynchronous? Is there any other method to run a PHP script without using system() function? Please help me. Thanks.

Community
  • 1
  • 1
brainless
  • 5,698
  • 16
  • 59
  • 82
  • 4
    Duplicate of this thread : http://stackoverflow.com/questions/5103528/ You can use the same solution I provided – JohnP Feb 26 '11 at 15:11
  • @JohnP: That did't work for me! can you give me an example. – brainless Feb 26 '11 at 15:48
  • 1
    What was the problem, any error message? Keep in mind you need to send the output of the script to a file (or any other output stream) using the $output and $return_var params. Simply opening up a file descriptor with fopen and passing that should do it. – JohnP Feb 26 '11 at 16:09

1 Answers1

5

Add an & to the end of the command-line to make the process run in the background on the vast majority of shells. i.e.:

Script 1

<?php
    echo "Entering main script 1";
    system("php script2.php &");
    echo "Exiting main script 1";
?>
tobyodavies
  • 27,347
  • 5
  • 42
  • 57