0

How can I execute an external binary from within my php code, without the page waiting for a return value before it is sent?

Let me clarify. I have a web application that needs to do some fairly cpu intensive tasks, like a lot of file IO amongst other things. I want my user to be able to initiate their task from the GUI in their browser, but I then want my program to hand the task over to a separate file on my server so my user does not have to sit and wait for all the work to be done and risking timing out his/her connection etc.

Let me show you a simple example of what I have tried:

index.php

<?php
    echo "This is a test run for an eternal program: <br><br>";
    echo shell_exec("hello");

    //ok so we can run executables from php script YAY .. but what about if they are long programs?
    //If my script is waiting for a return value surely the connection will time out?

    echo "<br><br>";
    echo shell_exec("long_run"); //Waits for return value as expected. Further more it
                                     //just does not do the file IO

?>

hello

#!/usr/bin/php5

<?php
    echo "hello";
?>

long_run

#!/usr/bin/php5

<?php
    $f = fopen("time.txt","a");
    $i = 1;

    while ($i < 10) {
        sleep(2);
        echo $i . "<br>";
        fwrite($f, $i . " ");
        $i++;
    }

    fclose($f);
?>

Note: I wrote the long_run example in PHP for consistency in reality my program is a binary and needs to interface with the PHP that my actual website is written with.

I have contemplated solving my problem by simply having the PHP save instructions to a file which my binary can scan on a regular basis and act upon. I think this kind of interface would be easy enough to implement it, but as someone with limited experience in PHP and indeed in web projects of this nature generally I would really like to get a better idea of the 'correct' approach to this problem. I'm sure there must be a standard way?

Sam Redway
  • 7,605
  • 2
  • 27
  • 41

1 Answers1

0

You can achieve it by running your command in background just append & to the command you wich to run.

It will run the binary in a seperate process without a return value

<?php
    shell_exec("yourcommand &"); // It will run the 'yourcommand' in backgournd
    .... // All further operations will be executed right after the shell exec without waiting for it return value
BentoumiTech
  • 1,627
  • 14
  • 21
  • Actually I already tried this and it didn't work .. although you would imagine it should. Once thing I am really wondering is - why am I getting no file written? It will send the echo to std io - i.e. I can see them in the browser, but it is not writing the file. – Sam Redway Mar 15 '15 at 11:25
  • @SamRedway when you execute your command through the terminal does it write a file ? – BentoumiTech Mar 16 '15 at 12:05
  • Yes it does. It works as expected from the terminal - which leaves me to think that the the reason it is behaving differently when called from the browser client is related to the context in which the process is running. Ultimately I think that this is probably just the wrong approach to the problem and have decided I need to write an interface program between the web app and the binary which passes data via a file. – Sam Redway Mar 16 '15 at 12:26