首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >exec('top',$output)返回一个空数组吗?

exec('top',$output)返回一个空数组吗?
EN

Stack Overflow用户
提问于 2018-06-20 08:37:44
回答 1查看 443关注 0票数 2

我正在编写一个PHP脚本,用于检查我的系统的cpu使用情况,但是它不能像我想的那样工作。

我在这里看到了很多类似的问题,但是我还没有找到解决我的问题的方法。

代码语言:javascript
复制
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);
    }
}

此代码返回此输出:

代码语言:javascript
复制
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命令”的输出作为数组吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-20 08:58:33

Top是一个交互式程序,默认情况下将输出发送到视频。为了获得输出,可以使用-b-n 1 (按手册页)以批处理模式运行它。

代码语言:javascript
复制
-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.

代码语言:javascript
复制
-n  :Number-of-iterations limit as:  -n number
        Specifies the maximum number of iterations, or frames, top should produce before ending.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50943825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档