Question

When using php passthru() function, default is to stay until the external script execution ends.

PHP manual says ::::: "If a program is started with this 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." - http://php.net/manual/en/function.passthru.php

I want to collect external script data for specific time e.g. for 30 seconds, and after that continue the php script execution. Any ideas how to do that?

My current code is:

    ob_start();
    passthru("/home/somefolder/somefolder2/program $pic1 $pic2");
    $details = ob_get_contents();
    ob_end_clean();

problem in above code is - sometimes "program" script keeps executing for a while, but i don't want to let the user to hang-on more than 30 seconds on this process.

I thought about redirecting the output to a file and read it after 30 seconds(according to the manual). But i don't know how redirect the output to a file.

Was it helpful?

Solution

"/home/somefolder/somefolder2/program $pic1 $pic2 > /path/to/your/file 2>&1"

And if you want it run in the background, add & at the end.

"/home/somefolder/somefolder2/program $pic1 $pic2 > /path/to/your/file 2>&1 &"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top