Trying to run a Background PHP process on a web page that will return its output in realtime?
Try something like this example of a basic shell execution from PHP using proc_open():

$cmd = "ping";

$descriptorspec = array(
   0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
   2 => array("pipe", "w")    // stderr is a pipe that the child will write to
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
echo "</pre>";


if you want to use the PHP exec(), you can send output to a file and read the last line of output file using ajax calls at regular intervals or using SSE:

$result = exec('$cmd > /path/to/file &');

The ampersand at the end will make it run in the background and you can read the output in /path/to/file

Book Your Free Consultation

Submit your name and email below to schedule your free 15 minute initial consultation to see how we can help your business succeed