获取CPU利用率信息
如何获取 Linux 中进程的 CPU 利用率和时间信息?基本上我想让我的应用程序过夜运行。同时,我想监控应用程序运行期间的CPU利用率。
我试过了grep 应用程序名称 >&日志,它似乎没有在日志中返回任何内容。有人可以帮我解决这个问题吗?
谢谢。
How could I get the CPU utilization with time info of a process in linux? Basically I want to let my application run overnight. At the same time, I would like to monitor the CPU utilization during the period the application is run.
I tried top | grep appName >& log, it does not seem to return me anything in the log. Could someone help me with this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
vmstat 和 iostat 都可以为您提供这种性质的定期信息;我建议手动设置次数,或者将单个轮询放入 cron 作业中,然后将输出重定向到文件:
这将为您提供 24 小时内每 20 秒一次的使用情况快照。
vmstat and iostat can both give you periodic information of this nature; I would suggest either setting up the number of times manually, or putting a single poll into a cron job, and then redirecting the output to a file:
This would give you a snapshot of usage every 20 seconds for 24 hours.
安装 sysstat 包并运行 sar
install sysstat package and run sar
使用 top 或 watch 命令
use the top or watch command
编写一个程序来调用您的进程,然后调用 getrusage(2) 并报告其子进程的统计信息。
Write a program that invokes your process and then calls getrusage(2) and reports statistics for its children.
您可以在程序运行时使用
top
监视程序使用的时间。或者,您可以使用
time
命令启动应用程序,该命令将打印程序在执行结束时使用的 CPU 时间总量。只需输入time ./my_app
而不是仅输入./my_app
有关更多信息,
man 1 time
You can monitor the time used by your program with
top
while it is running.Alternatively, you can launch your application with the
time
command, which will print the total amount of CPU time used by your program at the end of its execution. Just typetime ./my_app
instead of just./my_app
For more info,
man 1 time