linux 如何获取proc_open()的输出

zqdjd7g9  于 2024-01-06  发布在  Linux
关注(0)|答案(3)|浏览(160)

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

  1. $descriptorspec = array(
  2. 0 => array("pipe", "r"),
  3. 1 => array("pipe", "w"),
  4. 2 => array("file", "files/temp/error-output.txt", "a")
  5. );
  6. $process = proc_open("time ./a a.out", $descriptorspec, $pipes, $cwd);

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

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


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

tnkciper

tnkciper1#

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

  1. [edan@edan tmp]$ cat proc.php
  2. <?php
  3. $cwd='/tmp';
  4. $descriptorspec = array(
  5. 0 => array("pipe", "r"),
  6. 1 => array("pipe", "w"),
  7. 2 => array("file", "/tmp/error-output.txt", "a") );
  8. $process = proc_open("time ./a a.out", $descriptorspec, $pipes, $cwd);
  9. echo stream_get_contents($pipes[1]);
  10. fclose($pipes[1]);
  11. ?>
  12. [edan@edan tmp]$ php proc.php
  13. a.out here.
  14. [edan@edan tmp]$ cat /tmp/error-output.txt
  15. real 0m0.001s
  16. user 0m0.000s
  17. sys 0m0.002s

字符串

展开查看全部
jljoyd4f

jljoyd4f2#

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

  1. set_time_limit(1800);
  2. ob_implicit_flush(true);
  3. $exe_command = 'C:\\Windows\\System32\\ping.exe -t google.com';
  4. $descriptorspec = array(
  5. 0 => array("pipe", "r"), // stdin
  6. 1 => array("pipe", "w"), // stdout -> we use this
  7. 2 => array("pipe", "w") // stderr
  8. );
  9. $process = proc_open($exe_command, $descriptorspec, $pipes);
  10. if (is_resource($process))
  11. {
  12. while( ! feof($pipes[1]))
  13. {
  14. $return_message = fgets($pipes[1], 1024);
  15. if (strlen($return_message) == 0) break;
  16. echo $return_message.'<br />';
  17. ob_flush();
  18. flush();
  19. }
  20. }

字符串
希望能帮到你=)

展开查看全部
hs1rzwqc

hs1rzwqc3#

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

  1. /**
  2. * Executes process
  3. *
  4. * @param string $command
  5. * @param string $cwd
  6. * @param bool $exitOnError
  7. * @return void
  8. */
  9. function exec(string $command, string $cwd, bool $exitOnError = true): void
  10. {
  11. $descriptorSpec = array(
  12. 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
  13. 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
  14. 2 => array("pipe", "w"), // stderr is a pipe that the child will write to
  15. );
  16. $process = proc_open($command, $descriptorSpec, $pipes, $cwd);
  17. if (is_resource($process)) {
  18. do {
  19. $read = array($pipes[1], $pipes[2]);
  20. $write = null;
  21. $except = null;
  22. if (stream_select($read, $write, $except, 5)) {
  23. foreach ($read as $c) {
  24. if (feof($c)) {
  25. continue;
  26. }
  27. $read = fread($c, 1024);
  28. if ($read === false) {
  29. continue;
  30. }
  31. echo $read;
  32. }
  33. }
  34. } while (!feof($pipes[1]) | !feof($pipes[2]));
  35. fclose($pipes[0]);
  36. fclose($pipes[1]);
  37. fclose($pipes[2]);
  38. // It is important to close any pipes before calling
  39. // proc_close to avoid a deadlock
  40. $returnValue = proc_close($process);
  41. if ($returnValue != 0) {
  42. if ($exitOnError) {
  43. exit(1);
  44. }
  45. }
  46. } else {
  47. echo "Couldn't open $command";
  48. if ($exitOnError) {
  49. exit(1);
  50. }
  51. }
  52. }

字符串

展开查看全部

相关问题