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

前端 未结 21 1841
抹茶落季
抹茶落季 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:49

    The following command gets the average of CPU and memory usage every 40 seconds for a specific process(pid)

    pidstat 40 -ru -p <pid>
    

    Output for my case(first two lines for CPU usage, second two lines for memory):

    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
    
    0 讨论(0)
  • 2020-11-27 09:50

    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.

    0 讨论(0)
  • 2020-11-27 09:51

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

    top -c a -pid PID
    

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

    0 讨论(0)
  • 2020-11-27 09:54
    ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr
    

    or per process

    ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr |grep mysql
    
    0 讨论(0)
  • 2020-11-27 09:55

    Based on @Neon answer, my two cents here:

    pidstat -h -r -u -v -p $(ps aux | grep <process name> | awk '{print $2}' | tr '\n' ',')
    
    0 讨论(0)
  • 2020-11-27 09:56

    Above list out the top cpu and memory consuming process

            ps axo %cpu,%mem,command | sort -nr | head
    
    0 讨论(0)
提交回复
热议问题