As already mentioned in the comments to your question you need to fork a process in such way that you can communicate with it. That is not possible with imple functions like exec()
and the like.
Instead take a look at this simple example:
File test.sh
:
#!/bin/bash
echo start counting ...
for counter in 1 2 3
do
echo * counter is at value $counter *
sleep 3
done
echo ... finished counting.
File test.php
:
<?php
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
);
echo "forking process ...\n";
$process = proc_open('./test.sh', $descriptorspec, $pipes);
if (is_resource($process)) {
echo "... successfully forked process ...\n";
while (!feof($pipes[1])) {
echo fread($pipes[1], 1024);
flush();
}
echo "... process has finished ...\n";
fclose($pipes[0]);
fclose($pipes[1]);
echo "... pipes closed ...\n";
proc_close($process);
echo "... process closed.\n";
} else {
echo "... failed to fork process!\n";
}
The obvious output of a test run of that php script is:
forking process ...
... successfully forked process ...
start counting ...
* counter is at value 1 *
* counter is at value 2 *
* counter is at value 3 *
... finished counting.
... process has finished ...
... pipes closed ...
... process closed.
But the interesting part here is that this output is not sent in one go once that forked process has finished, but in what you referred to as "a live manner". So the first four lines appear immediately, the next two with a 3 second delay each, then the rest of the output.
Please note that the above example is meant as a demonstration for a Linux CLI environment. So it does not care about html markup but outputs plain text and relies on bash
as a shell environment for the forked process. You will have to adapt that simple demonstration to your needs, obviously.