fork download
  1. function rtprocess($commands = array()){
  2. ini_set('zlib.output_compression', 0);
  3. ini_set('implicit_flush', 1);
  4.  
  5. $pipes = [];
  6. $processes = [];
  7. $output = [];
  8. foreach($commands as $key => $command){
  9. $descriptorspec[$key] = array(
  10. 0 => array('pipe', 'r'),
  11. 1 => array('pipe', 'w'),
  12. 2 => array('pipe', 'w')
  13. );
  14. $processes[] = proc_open($command, $descriptorspec[$key], $procPipes);
  15. stream_set_blocking($procPipes[1], 0);
  16. $pipes[$key] = $procPipes;
  17. $output[$key] = '';
  18. }
  19.  
  20. echo '<pre>';
  21. do{
  22. $allFinished = true;
  23. foreach($processes as $key => $process){
  24. $returnedValues = proc_get_status($process);
  25. if($returnedValues['running']){
  26. $allFinished = false;
  27. $s = fread($pipes[$key][1],1024);
  28. if($s){
  29. $output[$key] .= $s;
  30. print($s);
  31. flush();
  32. }
  33.  
  34. }
  35. }
  36. } while($allFinished == false);
  37. echo '</pre>';
  38.  
  39. foreach($processes as $process){
  40. proc_close($process);
  41. }
  42.  
  43. foreach($output as $o){
  44. echo '<pre>'.print_r($o,true).'</pre>';
  45. }
  46. }
  47.  
  48. $commands = array();
  49. $commands[] = "ping -n 5 127.0.0.1 2>&1";
  50. $commands[] = "ping -n 5 10.0.0.1 2>&1";
  51. //rtprocess($commands);
Success #stdin #stdout 0.04s 25272KB
stdin
Standard input is empty
stdout
function rtprocess($commands = array()){
    ini_set('zlib.output_compression', 0);
    ini_set('implicit_flush', 1);
    ob_implicit_flush(1);
    
    $pipes = [];
    $processes = [];
    $output = [];
    foreach($commands as $key => $command){
        $descriptorspec[$key] = array(
            0 => array('pipe', 'r'),
            1 => array('pipe', 'w'),
            2 => array('pipe', 'w')
        );
        $processes[] = proc_open($command, $descriptorspec[$key], $procPipes);
        stream_set_blocking($procPipes[1], 0);
        $pipes[$key] = $procPipes;
        $output[$key] = '';
    }
    
    echo '<pre>';
    do{
        $allFinished = true;
        foreach($processes as $key => $process){
            $returnedValues = proc_get_status($process);
            if($returnedValues['running']){
                $allFinished = false;
                $s = fread($pipes[$key][1],1024);
                if($s){
                    $output[$key] .= $s;
                    print($s);
                    ob_flush();
                    flush();
                }
                
            }
        }
    } while($allFinished == false);
    echo '</pre>';
    
    foreach($processes as $process){
        proc_close($process);
    }
    
    foreach($output as $o){
        echo '<pre>'.print_r($o,true).'</pre>';
    }
}

$commands = array();
$commands[] = "ping -n 5 127.0.0.1 2>&1";
$commands[] = "ping -n 5 10.0.0.1 2>&1";
//rtprocess($commands);