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
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
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.
(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)
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
Based on @Neon answer, my two cents here:
pidstat -h -r -u -v -p $(ps aux | grep <process name> | awk '{print $2}' | tr '\n' ',')
Above list out the top cpu and memory consuming process
ps axo %cpu,%mem,command | sort -nr | head