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

前端 未结 21 1974
抹茶落季
抹茶落季 2020-11-27 09:00

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

21条回答
  •  佛祖请我去吃肉
    2020-11-27 09:57

    All of the answers here show only the memory percentage for the 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:

    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 %:

    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
    

提交回复
热议问题