Php 为什么Linux命令dmidecode在exec中使用时不返回任何内容?
如果在终端中运行命令“dmidecode-s处理器频率”,则显示信息:2000 MHz。 但当我运行PHP脚本时:Php 为什么Linux命令dmidecode在exec中使用时不返回任何内容?,php,linux,terminal,debian,exec,Php,Linux,Terminal,Debian,Exec,如果在终端中运行命令“dmidecode-s处理器频率”,则显示信息:2000 MHz。 但当我运行PHP脚本时: exec("dmidecode -s processor-frequency", $output); print_r($output); 它什么也不返回 更新: 示例中的命令返回1行。在这种情况下,我们可以使用exec 是否可以使用此应用程序路径而不是dmidecode? 但是路径是什么?的$output只包含结果的最后一行: 命令结果的最后一行。如果您需要执行命令并将命令中的所
exec("dmidecode -s processor-frequency", $output);
print_r($output);
它什么也不返回
更新:示例中的命令返回1行。在这种情况下,我们可以使用exec
是否可以使用此应用程序路径而不是dmidecode? 但是路径是什么?的
$output
只包含结果的最后一行:
命令结果的最后一行。如果您需要执行命令并将命令中的所有数据直接传回而不受任何干扰,请使用passthru()
函数
如果要捕获完整输出,请使用或
passthru
$command = "dmidecode -s processor-frequency";
passthru($command, $output);
echo $output;
$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);
proc\u open
$command = "dmidecode -s processor-frequency";
passthru($command, $output);
echo $output;
$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);
的$output
仅包含结果的最后一行:
命令结果的最后一行。如果您需要执行命令并将命令中的所有数据直接传回而不受任何干扰,请使用passthru()
函数
如果要捕获完整输出,请使用或
passthru
$command = "dmidecode -s processor-frequency";
passthru($command, $output);
echo $output;
$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);
proc\u open
$command = "dmidecode -s processor-frequency";
passthru($command, $output);
echo $output;
$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);
@NazarVozniy,那么命令返回
error 127
string。如果这不是您所期望的,那么问题应该在命令本身。我刚刚演示了如何捕获stdout
和stderr
@NazarVozniy,然后命令返回error 127
string。如果这不是您所期望的,那么问题应该在命令本身。我刚刚演示了如何捕获stdout
和stderr
。