I am encoding a web page with a PHP script designed to receive the name of the JFFS2 image file that was previously uploaded to the server. The script is then to re-play the section on the server with the image and output the results. I used this:
$tmp = shell_exec("update_flash -v " . $filename . " 4 2>&1"); echo '<h3>' . $tmp . '</h3>'; echo verifyResults($tmp);
(The verifyResults function will return some HTML code that tells the user whether the update command completed successfully. I., if the update completed successfully, display a button to restart the device, etc.)
The problem is that it takes a few minutes to execute the update command, and the PHP script will block until the shell command completes before it returns any output. This usually means that the update command will continue while the user sees an HTTP 504 error (in the worst case) or wait for the page to load within a few minutes.
I was thinking of doing something like this:
shell_exec("rm /tmp/output.txt"); shell_exec("update_flash -v " . $filename . " 4 2>&1 >> /tmp/output.txt &"); echo '<div id="output"></div>'; echo '<div id="results"></div>';
This would theoretically put the command in the background and add all the output to /tmp/output.txt.
And then, in the Javascript function, I would periodically request getOutput.php, which would simply print the contents of /tmp/output.txt and paste it into the "output" div. Once the command is fully executed, another Javascript function will process the output and display the result in the "results" div.
But the problem that I see here is that getOutput.php will eventually become unavailable when updating the flash memory of the device, because it is on the segment for which the update is intended. Thus, this may leave me in the same position as before, although without page 504 or, it would seem, forever loaded page.
I could move getOutput.php to another section on the device, but then I think that I still have to do some funky things with the web server configuration in order to be able to access it there (symbolic link to it from a web browser like any other file, it will eventually be overwritten during the second flash).
Is there any other way to display the output of a command during its launch, or do I need to do this with my solution?
Edit 1: I am currently testing some solutions. I will update my question later.
Edit 2: It seems that the file system is not overwritten as I originally thought. Instead, the system seems to mount the existing file system in read-only mode, so I can still access getOutput.php even after restarting the file system.
The second solution I described in my question seems to work in addition to using popen (as indicated in the answer below) instead of shell_exec. The page loads, and through Ajax I can display the contents of output.txt.
However , it seems that output.txt does not reflect the output of the re-flash command in real time - it seems that nothing is displayed until the update command returns from execution. I will need to do additional testing to find out what is going on here.
Edit 3: Ignore it, it looks like the file is working when I access it. I simply delayed the delay while the kernel performed some JFFS2-related tasks caused by my use of the partition on which the original JFFS2 image is stored. I don't know why, but this seems to cause all PHP scripts to block until it ends.
To get around this, I am going to include the call of the update command in a separate script and request it via Ajax - this way the user will at least get some pre-packaged feedback while technically still waiting on the system.