I'm attempting to run a process on a ubuntu server from PHP but it appears to hang up when you try it with multiple tabs open, although it seems to work on multiple devices. The code is:
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);
ini_set('implicit_flush', true);
ob_implicit_flush(true);
while (ob_get_level() > 0)
{
$level = ob_get_level();
ob_end_clean();
if (ob_get_level() == $level) break;
}
if (function_exists('apache_setenv'))
{
apache_setenv('no-gzip', '1');
apache_setenv('dont-vary', '1');
}
$command = 'ping -c 5 8.8.8.8';
$cwd = '/tmp';
$descriptorspec =
[
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w']
];
$process = proc_open($command, $descriptorspec, $pipes, $cwd);
$proc_details = proc_get_status($process);
$pid = $proc_details['pid'];
$_SESSION['upid'] = $pid;
if (is_resource($process))
{
while ($s = fgets($pipes[1]))
{
print $s;
flush();
}
}
This produces the desired effect of running the ping command and outputting the result each line at a time but if I open up another tab and run it, the second tab won't run until the command in the first is finished.
How can I prevent the waiting and have both tabs run in parallel?