php - 为什么 Linux 命令 dmidecode 在 exec 中使用时不返回任何内容?

标签 php linux terminal debian exec

如果在终端运行命令“dmidecode -s processor-frequency”,会显示信息:2000 MHz。 但是当我运行 PHP 脚本时:

exec("dmidecode -s processor-frequency", $output);
print_r($output);

它什么都不返回。

更新:
示例中的命令返回 1 行。在这种情况下,我们可以使用exec
也许不是 dmidecode 来使用这个应用程序路径? 但是路径是什么?

最佳答案

exec$output 仅包含结果的最后一行:

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

如果要捕获完整输出,请使用 passthruproc_open .

直通

$command = "dmidecode -s processor-frequency";
passthru($command, $output);
echo $output;

proc_open

$command = "dmidecode -s processor-frequency";
$desc = [ 1 => ['pipe', 'w'], 2 => ['pipe', 'w'] ];
$proc = proc_open($command, $desc, $pipes);

if (!is_resource($proc)) {
  fprintf(STDERR, "command failed: $command\n");
  exit(1);
}

if ($output = stream_get_contents($pipes[1])) {
  fprintf(STDOUT, "STDOUT: %s\n", $output);
}

if ($error = stream_get_contents($pipes[2])) {
  fprintf(STDERR, "STDERR: %s\n", $error);
}

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

proc_close($proc);

关于php - 为什么 Linux 命令 dmidecode 在 exec 中使用时不返回任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36976413/

相关文章:

linux - 尝试在 Windows 10 上为 Linux 构建 Go 代码时出错

terminal - 与 Digilent Atlys 板的串行通信

php - 组织这种结构的最佳方式?

php - 如何提高bemusic服务器文件上传限制?

c# - 任何c#应用程序都可以在linux上运行吗

C++ : initialize input programmatically

mysql - SQL 请求 CREATE TEMPORARY TABLES(在终端中工作,但不通过 php)

php - 收听数据库插入并向 Android 设备发送推送通知

php - 将选中的项目从 CheckedListBox 转换为字符串,然后将其存储到 MySQL 数据库

c++ - 如何在Linux下编译DEC UNIX v4.0应用程序源代码?