linux 如何获取proc_open()的输出

zqdjd7g9  于 11个月前  发布在  Linux
关注(0)|答案(3)|浏览(120)

我试着从php中的proc_open方法中获取输出,但是,当我打印它时,我得到了空的。

$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("file", "files/temp/error-output.txt", "a")
);

$process = proc_open("time ./a  a.out", $descriptorspec, $pipes, $cwd);

字符串
只要我知道,我就可以用stream_get_contents()得到输出

echo stream_get_contents($pipes[1]);
fclose($pipes[1]);


但我不能这么做,有什么建议吗?
Thx before.

tnkciper

tnkciper1#

您的代码或多或少对我有用。time将其输出打印到stderr,因此如果您正在查找该输出,请查看文件files/temp/error-output.txtstdout管道$pipes[1]将仅包含程序./a的输出。
我的复制品:

[edan@edan tmp]$ cat proc.php 

<?php

$cwd='/tmp';
$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("file", "/tmp/error-output.txt", "a") );

$process = proc_open("time ./a a.out", $descriptorspec, $pipes, $cwd);

echo stream_get_contents($pipes[1]);
fclose($pipes[1]);

?>

[edan@edan tmp]$ php proc.php 

a.out here.

[edan@edan tmp]$ cat /tmp/error-output.txt

real    0m0.001s
user    0m0.000s
sys     0m0.002s

字符串

jljoyd4f

jljoyd4f2#

这是proc_open()的另一个例子。在这个例子中,我使用Win32 ping.exe命令。CMIIW

set_time_limit(1800);
ob_implicit_flush(true);

$exe_command = 'C:\\Windows\\System32\\ping.exe -t google.com';

$descriptorspec = array(
    0 => array("pipe", "r"),  // stdin
    1 => array("pipe", "w"),  // stdout -> we use this
    2 => array("pipe", "w")   // stderr 
);

$process = proc_open($exe_command, $descriptorspec, $pipes);

if (is_resource($process))
{

    while( ! feof($pipes[1]))
    {
        $return_message = fgets($pipes[1], 1024);
        if (strlen($return_message) == 0) break;

        echo $return_message.'<br />';
        ob_flush();
        flush();
    }
}

字符串
希望能帮到你=)

hs1rzwqc

hs1rzwqc3#

这里是一个完整的函数,它读取stdout和stderr。

/**
 * Executes process
 *
 * @param string $command
 * @param string $cwd
 * @param bool $exitOnError
 * @return void
 */
function exec(string $command, string $cwd, bool $exitOnError = true): void
{
    $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($command, $descriptorSpec, $pipes, $cwd);

    if (is_resource($process)) {
        do {
            $read = array($pipes[1], $pipes[2]);
            $write = null;
            $except = null;

            if (stream_select($read, $write, $except, 5)) {
                foreach ($read as $c) {
                    if (feof($c)) {
                        continue;
                    }
                    $read = fread($c, 1024);

                    if ($read === false) {
                        continue;
                    }

                    echo $read;
                }
            }
        } while (!feof($pipes[1]) | !feof($pipes[2]));

        fclose($pipes[0]);
        fclose($pipes[1]);
        fclose($pipes[2]);

        // It is important to close any pipes before calling
        // proc_close to avoid a deadlock
        $returnValue = proc_close($process);

        if ($returnValue != 0) {
            if ($exitOnError) {
                exit(1);
            }
        }

    } else {
        echo "Couldn't open $command";
        if ($exitOnError) {
            exit(1);
        }
    }
}

字符串

相关问题