如何显示' top'的输出使用Php在Web浏览器上命令

时间:2017-06-20 12:17:06

标签: php shell-exec

我有一个简单的PHP脚本' top.php'使用shell_exec函数。

代码:

<?php
echo shell_exec("top");
?>

我正在寻找的是查看&#39; top&#39;的结果。在Web浏览器上执行命令。所以,如果我访问http://192.168.1.1/top.php,我想查看top命令的结果。必须定期查看top命令的结果,就像在命令行终端中看到的那样。

然而,当我访问&#39; http://192.168.1.1/top.php&#39;在Web浏览器上,它不显示任何内容。在命令行执行top.php时的行为相同(&#39; php top.php&#39;)。

我不确定它出错的地方或地点......

1 个答案:

答案 0 :(得分:2)

top on the command line by default just keeps running, so I suspect what's happening here is that it's not exiting and returning output to PHP. The -n 1 flag should address that:

<?php
echo shell_exec("top -n 1");
?>

This would give you one "page" of output from top to display on your webpage. In order to refresh it, you would of course just refresh the webpage.

To make something a little smoother (where you don't have to refresh the page), you could have a page which makes an AJAX request to this PHP script and then displays the output on the page. That AJAX request could then be scheduled with setInterval() in JavaScript to occur ever X seconds as you see fit.