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

时间:2009-08-03 10:13:51

标签: linux shell memory-management cpu-usage

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

22 个答案:

答案 0 :(得分:200)

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

(您可以不使用“cmd”,但这可能有助于调试)。

请注意,这会显示进程在运行时的平均CPU使用率。

答案 1 :(得分:59)

caf's answer的变体: top -p <pid>

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

答案 2 :(得分:31)

您可以使用

按流程名称获取结果
ps -C chrome -o %cpu,%mem,cmd

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

答案 3 :(得分:23)

使用pidstat(来自sysstat - Refer Link)。

e.g。使用

每5秒监视这两个进程ID(12345和11223)
$ pidstat -h -r -u -v -p 12345,11223 5

答案 4 :(得分:9)

启动程序并对其进行监控

如果您想轻松地对可执行文件进行基准测试,则此表单非常有用:

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

现在,当您按Ctrl + C时,它会退出程序并停止监控。样本输出:

CPU  MEM
20.0  1.3
35.0  1.3
40.0  1.3

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

在Ubuntu 16.04上测试。

答案 5 :(得分:5)

如上面caf's answer所述,ps和某些情况下pidstat会给你pCPU的生命周期平均值。 要获得更准确的结果,请使用top。如果您需要在运行后运行top:

top -b -n 1 -p <PID>

或仅用于处理数据和标题:

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

没有标题:

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

答案 6 :(得分:4)

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

答案 7 :(得分:4)

你可以使用top -b并grep out你想要的pid(在批处理模式下以-b标志顶部运行),或者也可以使用-p标志并指定pid而不使用grep的。

答案 8 :(得分:3)

ps命令(不应使用):

top命令(应使用):

使用top实时获取CPU使用率(当前短间隔):

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

回显类似:78.6

答案 9 :(得分:2)

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

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

答案 10 :(得分:1)

要仅获取应用程序的内存使用量(与其使用的共享库相反,您需要使用Linux smaps界面)。 This answer explains it well

答案 11 :(得分:1)

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

PID - 进程ID

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

%cpu - CPU使用率

%mem - 内存使用

cmd - 命令

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

答案 12 :(得分:1)

此处的所有答案仅显示PID的内存百分比。

以下是如何以KB为单位获取所有apache进程的rss内存使用情况的示例,替换&#34; grep apache&#34;用&#34; grep PID&#34;如果您只想观看特定的PID:

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

打印:

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

使用CPU%:

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

输出:

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

答案 13 :(得分:1)

根据@caf的回答,这对我来说很好。

计算给定PID的平均值:

measure.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"

用法:

# send PID as argument
sh measure.sh 3282

答案 14 :(得分:1)

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

答案 15 :(得分:0)

基于 this answer,我们可以通过收集 N samplessampling period T 来估计特定进程在特定时间内的平均 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) 
            }';

为了能够评估结果,我们将顶部的输出收集到 /var/tmp/foo.log 文件中。预期的输出是这样的:

2.33333 6.9

以及我们日志文件的内容:

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

请注意,我们忽略 (tail -n +2) top 命令的第一次执行。

答案 16 :(得分:0)

根据@Neon的答案,我这里有2美分:

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

答案 17 :(得分:0)

以下命令获取特定进程(pid)每40秒平均CPU和内存使用情况

pidstat 40 -ru -p <pid>

针对我的情况的输出(前两行用于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

答案 18 :(得分:0)

对于那些挣扎了一段时间的人,想知道为什么所选答案不起作用:

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

%cpu,%mem之间没有空格。

答案 19 :(得分:0)

上面列出了顶级cpu和内存消耗过程

apply plugin: 'kotlin-android-extensions'

答案 20 :(得分:0)

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

top -c a -pid PID

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

答案 21 :(得分:-2)

在Linux上单个进程的CPU和内存使用率,或者使用以下命令可以获取使用率最高的10个CPU

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

相关问题