Writing a CPU/RAM usage log over a period of time to file on CentOS
The standard ps
is enough.
while true; do ps o pcpu,rsz -p $pid | tail -n1 >>usage.log; sleep $interval; done
result:
0.0 3352
0.3 31640
0.4 36924
0.5 36052
...
First field is CPU usage in %, second is physical memory usage in kbytes.
If you care about precise timing and want CPU in percentage:
watch --precise -n 1 'top -b -n 1 -p [PID] | tail -n 1 | awk "{print \$9}" >> [PID].log'