我正在编写一个PHP脚本,用于检查我的系统的cpu使用情况,但是它不能像我想的那样工作。
我在这里看到了很多类似的问题,但是我还没有找到解决我的问题的方法。
public static function checkCurrentCpuUsage()
{
$load = sys_getloadavg();
if (true) {
$output = '';
exec('top', $output, $return_var);
echo "<br>";
echo 'print_r($load) --> ';
print_r($load);
echo "<br>";
echo 'var_dump($output) --> ';
var_dump($output);
echo "<br>";
echo 'print_r($output) --> ';
print_r($output);
echo "<br>";
echo 'print_r($return_var) --> ';
print_r($return_var);
}
}此代码返回此输出:
print_r($load) --> Array ( [0] => 0.53 [1] => 0.41 [2] => 0.41 )
var_dump($output) --> array(0) { }
print_r($output) --> Array ( )
print_r($return_var) --> 1为什么$output是空的?
在$output中,我尝试了很多方法来获得top命令作为数组的输出,但是没有什么效果。
我发现的一些答案告诉我,"exec('top',$output,$return_var)“会被困在"top -命令”中,所以我尝试使用“top -n 1”代替,但这对我没有用。
我还尝试了许多版本,使用"passthru()“和"shell-exec()”而不是"exec()",但对我来说没有什么效果。
您能告诉我如何将“top命令”的输出作为数组吗?
发布于 2018-06-20 08:58:33
Top是一个交互式程序,默认情况下将输出发送到视频。为了获得输出,可以使用-b和-n 1 (按手册页)以批处理模式运行它。
-b :Batch-mode operation
Starts top in Batch mode, which could be useful for sending output from top to other pro‐
grams or to a file. In this mode, top will not accept input and runs until the iterations
limit you've set with the `-n' command-line option or until killed.和
-n :Number-of-iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top should produce before ending.https://stackoverflow.com/questions/50943825
复制相似问题