如果在终端运行命令“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.
如果要捕获完整输出,请使用 passthru
或 proc_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/