4 php
我正在调用proc_open(),我无法捕获写入stderr的进程的输出:
$curFolder = getcwd();
$procDescriptor = array( 2 => array( "pipe", "w" ) );
$cmd = "MyApp.exe -f optimization.csv";
$process = proc_open( $cmd, $procDescriptor, $pipes, $curFolder );
if( is_resource( $process ) == true )
{
$procStatus = proc_get_status( $process );
while( $procStatus['running'] === true )
{
if( !feof( $pipes[2] ) )
{
$logLine = fgets( $pipes[2] );
echo( "Read >${logLine}<" );
}
sleep( 1 );
}
}
Run Code Online (Sandbox Code Playgroud)
该程序挂起在fgets()上.如果我从命令行运行程序,一切正常,即有一些东西写入stderr(我也试过使用stdout得到相同的结果).我在Windows上运行脚本 - Linux上的相同脚本运行顺利.
你有一个无限循环.
$ procStatus ["运行"]永远不会改变,除非你把电话给proc_get_status()内循环.PHP没有像JavaScript这样的动态属性.
我添加了这条线
$procStatus = proc_get_status( $process );
Run Code Online (Sandbox Code Playgroud)
睡觉后(),它工作正常.
来自proc_open() 文档:
<?php
$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("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$cwd = '/tmp';
$env = array('some_option' => 'aeiou');
$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
fwrite($pipes[0], '<?php print_r($_ENV); ?>');
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
echo "command returned $return_value\n";
}
?>
Run Code Online (Sandbox Code Playgroud)
如果您发现它是空的,也许您的进程不会向 stderr 报告