获取CPU利用率信息

发布于 2024-11-12 16:52:52 字数 155 浏览 4 评论 0原文

如何获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

兮子 2024-11-19 16:52:52

vmstatiostat 都可以为您提供这种性质的定期信息;我建议手动设置次数,或者将单个轮询放入 cron 作业中,然后将输出重定向到文件:

vmstat 20 4230 >> cpu_log_file

这将为您提供 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:

vmstat 20 4230 >> cpu_log_file

This would give you a snapshot of usage every 20 seconds for 24 hours.

沫离伤花 2024-11-19 16:52:52

安装 sysstat 包并运行 sar

nohup sar -o output.file 12 8 >/dev/null 2>&1 &

install sysstat package and run sar

nohup sar -o output.file 12 8 >/dev/null 2>&1 &
弥繁 2024-11-19 16:52:52

使用 top 或 watch 命令

PID    COMMAND      %CPU TIME     #TH  #WQ  #PORT #MREG RPRVT  RSHRD  RSIZE  VPRVT  VSIZE  PGRP  PPID  STATE    UID  FAULTS    COW    MSGSENT    MSGRECV    SYSBSD    SYSMACH   CSW       PAGEINS USER
10764  top          8.4  00:01.04 1/1  0    24    33    2000K  244K   2576K  17M    2378M      10764 10719 running  0    9908+     54     564790+    282365+    3381+     283412+   838+          27      root
10763  taskgated    0.0  00:00.00 2    0    25    27    432K   244K   1004K  27M    2387M  10763 1     sleeping 0    376       60     140        60         160       109       11        0       root

use the top or watch command

PID    COMMAND      %CPU TIME     #TH  #WQ  #PORT #MREG RPRVT  RSHRD  RSIZE  VPRVT  VSIZE  PGRP  PPID  STATE    UID  FAULTS    COW    MSGSENT    MSGRECV    SYSBSD    SYSMACH   CSW       PAGEINS USER
10764  top          8.4  00:01.04 1/1  0    24    33    2000K  244K   2576K  17M    2378M      10764 10719 running  0    9908+     54     564790+    282365+    3381+     283412+   838+          27      root
10763  taskgated    0.0  00:00.00 2    0    25    27    432K   244K   1004K  27M    2387M  10763 1     sleeping 0    376       60     140        60         160       109       11        0       root
冰魂雪魄 2024-11-19 16:52:52

编写一个程序来调用您的进程,然后调用 getrusage(2) 并报告其子进程的统计信息。

Write a program that invokes your process and then calls getrusage(2) and reports statistics for its children.

随梦而飞# 2024-11-19 16:52:52

您可以在程序运行时使用 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 type time ./my_app instead of just ./my_app

For more info, man 1 time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文