简体   繁体   English

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

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

I want to get the CPU and memory usage of a single process on Linux - I know the PID.我想在 Linux 上获取单个进程的 CPU 和内存使用情况 - 我知道 PID。 Hopefully, I can get it every second and write it to a CSV using the 'watch' command.希望我可以每秒获取它并使用“watch”命令将其写入 CSV。 What command can I use to get this info from the Linux command-line?我可以使用什么命令从 Linux 命令行获取此信息?

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 使用率。

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 使用率,因此有利于监控。

You can get the results by the name of the process using您可以使用进程名称获取结果

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

the -C option allows you to use process name without knowing it's pid. -C选项允许您在不知道进程名称的情况下使用进程名称。

ps command (should not use): ps命令(不应使用):

top command (should use): top命令(应该使用):

Use top to get CPU usage in real time(current short interval):使用top实时获取 CPU 使用率(当前短时间间隔):

top -b -n 2 -d 0.2 -p 6962 | tail -1 | awk '{print $9}'

will echo like: 78.6会回78.678.6

Use pidstat (from sysstat -Refer Link ).使用 pidstat(来自 sysstat -请参阅链接)。

eg 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

Launch a program and monitor it启动程序并监视它

This form is useful if you want to benchmark an executable easily:如果您想轻松地对可执行文件进行基准测试,此表单很有用:

topp() (
  $* &>/dev/null &
  pid="$!"
  trap ':' INT
  echo 'CPU  MEM'
  while sleep 1; do ps --no-headers -o '%cpu,%mem' -p "$pid"; done
  kill "$pid"
)
topp ./myprog arg1 arg2

Now when you hit Ctrl + C it exits the program and stops monitoring.现在,当您按 Ctrl + C 时,它会退出程序并停止监视。 Sample output:示例输出:

CPU  MEM
20.0  1.3
35.0  1.3
40.0  1.3

Related: https://unix.stackexchange.com/questions/554/how-to-monitor-cpu-memory-usage-of-a-single-process相关: https : //unix.stackexchange.com/questions/554/how-to-monitor-cpu-memory-usage-of-a-single-process

Tested on Ubuntu 16.04.在 Ubuntu 16.04 上测试。

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

As commented in caf's answer above, ps and in some cases pidstat will give you the lifetime average of the pCPU.正如上面 caf的回答所评论的那样,ps 和在某些情况下 pidstat 将为您提供 pCPU 的平均寿命。 To get more accurate results use top.要获得更准确的结果,请使用 top。 If you need to run top once you can run:如果您需要运行 top 一次,您可以运行:

top -b -n 1 -p <PID>

or for process only data and header:或仅用于处理数据和标题:

top -b -n 1 -p <PID> | tail -3 | head -2

without headers:没有标题:

top -b -n 1 -p <PID> | tail -2 | head -1
ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr

或每个进程

ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr |grep mysql

The following command gets the average of CPU and memory usage every 40 seconds for a specific process(pid)以下命令获取特定进程(pid)每 40 秒的 CPU 和内存使用率平均值

pidstat 40 -ru -p <pid>

Output for my case(first two lines for CPU usage, second two lines for memory):我的情况的输出(前两行是 CPU 使用率,后两行是内存):

02:15:07 PM       PID    %usr %system  %guest    %CPU   CPU  Command
02:15:47 PM     24563    0.65    0.07    0.00    0.73     3  java

02:15:07 PM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
02:15:47 PM     24563      6.95      0.00 13047972 2123268   6.52  java

For those who struggled for a while wonderring why the selected answer does not work:对于那些挣扎了一段时间想知道为什么所选答案不起作用的人:

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

No SPACE ibetween %cpu, and %mem . %cpu,%mem没有空格。

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).要仅获取应用程序的内存使用情况(与它使用的共享库相反,您需要使用 Linux smaps 接口)。 This answer explains it well . 这个答案很好地解释了它

ps aux|awk  '{print $2,$3,$4}'|grep PID

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

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 等...

All of the answers here show only the memory percentage for the PID.此处的所有答案仅显示 PID 的内存百分比。

Here's an example of how to get the rss memory usage in KB for all apache processes, replace "grep apache" with "grep PID" if you just want to watch a specific PID:以下是如何获取所有 apache 进程的 rss 内存使用量(以 KB 为单位)的示例,如果您只想查看特定的 PID,请将“grep apache”替换为“grep PID”:

watch -n5 "ps aux -y | grep apache | awk '{print \$2,\$6}'"

This prints:这打印:

Every 5.0s: ps aux -y | grep apache | awk '{print $2,$6}'                                                                                                                                                                                                          
Thu Jan 25 15:44:13 2018

12588 9328
12589 8700
12590 9392
12591 9340
12592 8700
12811 15200
15453 9340
15693 3800
15694 2352
15695 1352
15697 948
22896 9360

With CPU %:使用 CPU %:

watch -n5 "ps aux -y | grep apache | awk '{print \$2,\$3,\$6}'"

Output:输出:

Every 5.0s: ps aux -y | grep apache | awk '{print $2,$3,$6}'                                                                                                                                                                                                       
Thu Jan 25 15:46:00 2018

12588 0.0 9328
12589 0.0 8700
12590 0.0 9392
12591 0.0 9340
12592 0.0 8700
12811 0.0 15200
15453 0.0 9340
15778 0.0 3800
15779 0.0 2352
15780 0.0 1348
15782 0.0 948
22896 0.0 9360

这是一个很好的技巧,可以实时跟踪一个或多个程序,同时观察其他工具的输出: watch "top -bn1 -p$(pidof foo),$(pidof bar); tool"

Based on @caf's answer, this working nicely for me.根据@caf 的回答,这对我来说效果很好。

Calculate average for given PID:计算给定 PID 的平均值:

measure.sh测量.sh

times=100
total=0
for i in $(seq 1 $times)
do
   OUTPUT=$(top -b -n 1 -d 0.1 -p $1 | tail -1 | awk '{print $9}')
   echo -n "$i time: ${OUTPUT}"\\r
   total=`echo "$total + $OUTPUT" | bc -l`
done
#echo "Average: $total / $times" | bc

average=`echo "scale=2; $total / $times" | bc`
echo "Average: $average"

Usage:用法:

# send PID as argument
sh measure.sh 3282

(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上尝试过)

上面列出了 CPU 和内存消耗最高的进程

        ps axo %cpu,%mem,command | sort -nr | head

根据@Neon 的回答,我的两分钱在这里:

pidstat -h -r -u -v -p $(ps aux | grep <process name> | awk '{print $2}' | tr '\n' ',')

Based on this answer we can estimate the average CPU and memory utilization of a specific process for a specific amount of time by collecting N samples with sampling period T as follows:基于这个答案,我们可以通过收集N samples sampling period T N samples来估计特定进程在特定时间内的平均 CPU 和内存利用率,如下所示:

N=3;
T=1;
PROCESS_NAME="my_proc";

top -b -c -n $(let tmp=N+1; echo $tmp) -d ${T} -p $(pgrep ${PROCESS_NAME}) | 
grep ${PROCESS_NAME} |  
tee /var/tmp/foo.log |
tail -n +2 | 
awk -v N=$N 'BEGIN{
                c=0; 
                m=0
            }{
                c=c+$9; 
                m=m+$10
            }END{
                printf("%s %s\n", c/N, m/N) 
            }';

In order to be able to evaluate the results we are collecting the output of the top into the /var/tmp/foo.log file.为了能够评估结果,我们将 top 的输出收集到/var/tmp/foo.log文件中。 The expected output is something like this:预期的输出是这样的:

2.33333 6.9

And the content of our log file:以及我们日志文件的内容:

196918 root      20   0   24.4g   1.3g 113872 S   0.0   6.9  39:58.15 my_proc
196918 root      20   0   24.4g   1.3g 113872 S   2.0   6.9  39:58.17 my_proc
196918 root      20   0   24.4g   1.3g 113872 S   3.0   6.9  39:58.20 my_proc
196918 root      20   0   24.4g   1.3g 113872 S   2.0   6.9  39:58.22 my_proc

Note that we ignore ( tail -n +2 ) the first execution of the top command.请注意,我们忽略( tail -n +2 )top 命令的第一次执行。

CPU and memory usage of a single process on Linux or you can get the top 10 cpu utilized processes by using below command Linux 上单个进程的 CPU 和内存使用率,或者您可以使用以下命令获取前 10 个 CPU 使用率的进程

ps -aux --sort -pcpu | ps -aux --sort -pcpu | head -n 11头-n 11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM