为什么不执行 exec(“top”);在 Linux 上工作?
我试图执行这个命令
echo exec("top");
,但
echo exec("/usr/bin/top");
都不起作用(返回空白输出)
有人知道为什么吗?
I was trying to execute this command
echo exec("top");
and
echo exec("/usr/bin/top");
neither works (returns blank output)
does anybody know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为 top 是一个交互式程序,旨在在终端上运行,而不是从脚本执行。您可能想要运行带参数的“ps”命令,该命令将按 CPU 利用率对输出进行排序。
http://www.devdaily.com/linux /unix-linux-process-memory-sort-ps-command-cpu
Because top is an interactive program that is meant to be run on a terminal, not be executed from a script. You are probably want to run the 'ps' command with arguments which will sort output by cpu utilization.
http://www.devdaily.com/linux/unix-linux-process-memory-sort-ps-command-cpu
您实际上可以调用 top 并回显其输出。对我有用的代码:
-b - 以批处理模式运行
-n 1 - 仅一次迭代
You actually can call top and echo its output. Code that worked for me:
-b - running in batch mode
-n 1 - only one iteration
它可能有效,但 exec() 不会返回任何内容。阅读手册:
exec()
但是你还有另一个问题:
top
不会自行退出。你不能在这里使用它,因为你需要发送中断信号(刚刚意识到:q
也可以)。一种解决方案是使
top
停止一次迭代后It probably works, but
exec()
doesn't return anything. Read the Manual:exec()
But you have another problem:
top
doesn't exit by itself. You cannot use it here, because you need to send the interrupt-signal (just realized:q
is ok too).One solution is to make
top
to stop after one iteration如果你想把它放在一个变量中:
If you want to put it in a variable :
我用的是:
100%减去空闲时间。
I used:
100% minus the idle time.