在 Linux 上检索单个进程的 CPU 使用率和内存使用率?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1221555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:35:05  来源:igfitidea点击:

Retrieve CPU usage and memory usage of a single process on Linux?

linuxshellmemory-managementcpu-usage

提问by Supertux

I want to get the CPU and memory usage of a single process on Linux - I know the PID. Hopefully, I can get it every second and write it to a CSV using the 'watch' command. What command can I use to get this info from the Linux command-line?

我想在 Linux 上获取单个进程的 CPU 和内存使用情况 - 我知道 PID。希望我可以每秒获取它并使用“watch”命令将其写入 CSV。我可以使用什么命令从 Linux 命令行获取此信息?

采纳答案by caf

ps -p <pid> -o %cpu,%mem,cmd

(You can leave off "cmd" but that might be helpful in debugging).

(您可以省略“cmd”,但这可能有助于调试)。

Note that this gives average CPU usage of the process over the time it has been running.

请注意,这给出了进程在运行期间的平均 CPU 使用率。

回答by Alberto Zaccagni

You could use top -band grep out the pid you want (with the -bflag top runs in batch mode), or also use the -pflag and specify the pid without using grep.

您可以使用top -b并 grep 输出所需的 pid(-b标志 top 在批处理模式下运行),或者也可以使用-p标志并指定 pid 而不使用 grep。

回答by Paul Biggar

To get the memory usage of just your application (as opposed to the shared libraries it uses, you need to use the Linux smaps interface). This answer explains it well.

要获得您的应用程序的内存使用情况(与它使用的共享库相反,您需要使用 Linux smaps 接口)。这个答案很好地解释了它

回答by Manki

A variant of caf's answer: top -p <pid>

caf 答案的一种变体: top -p <pid>

This auto-refreshes the CPU usage so it's good for monitoring.

这会自动刷新 CPU 使用率,因此有利于监控。

回答by Patel95

ps aux | awk '{print "\t"}' | sort | uniq -c | awk '{print " "" "}' | sort -nr

or per process

或每个进程

ps aux | awk '{print "\t"}' | sort | uniq -c | awk '{print " "" "}' | sort -nr |grep mysql

回答by Neon

Use pidstat (from sysstat - Refer Link).

使用 pidstat(来自 sysstat -请参阅链接)。

e.g. to monitor these two process IDs (12345 and 11223) every 5 seconds use

例如每 5 秒监视这两个进程 ID(12345 和 11223)使用

$ pidstat -h -r -u -v -p 12345,11223 5

回答by Kieleth

(If you are in MacOS 10.10, try the accumulative -c option of top:

(如果您使用的是 MacOS 10.10,请尝试 top 的累积 -c 选项:

top -c a -pid PID

(This option is not available in other linux, tried with Scientific Linux el6 and RHEL6)

(此选项在其他linux中不可用,在Scientific Linux el6和RHEL6上尝试过)

回答by amit

You can get the results by the name of the process using

您可以使用进程名称获取结果

ps -C chrome -o %cpu,%mem,cmd

the -Coption allows you to use process name without knowing it's pid.

-C选项允许您在不知道进程名称的情况下使用进程名称。

回答by Osama Al-Banna

ps aux|awk  '{print ,,}'|grep PID

where the first column is the PID,second column CPU usage ,third column memory usage.

其中第一列是 PID,第二列是 CPU 使用率,第三列是内存使用率。

回答by Siva KR

ps axo pid,etime,%cpu,%mem,cmd | grep 'processname' | grep -v grep

PID - Process ID

PID - 进程 ID

etime - Process Running/Live Duration

etime - 进程运行/实时持续时间

%cpu - CPU usage

%cpu - CPU 使用率

%mem - Memory usage

%mem - 内存使用

cmd - Command

cmd - 命令

Replace processname with whatever process you want to track, mysql nginx php-fpm etc ...

将 processname 替换为您要跟踪的任何进程,mysql nginx php-fpm 等...