Writing a CPU/RAM usage log over a period of time to file on CentOS
16,690
Solution 1
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.
Solution 2
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'
Solution 3
Related videos on Youtube
Author by
keponk
Updated on September 17, 2022Comments
-
keponk over 1 year
I'm looking for an application or line of code that would let me observe a process, save the info in a number of variables, then put the gathered info on a file.
I've been trying with variations of
top
but no luck. I am running several CentOS virtual servers, VM is a 2GB RAM, 2 processor.A script that works over a specified amount of time while writing lines with the info on a text file so at the end I can have a sort of table with the data would work.
I'm going to stress test the server, and I would like to have the data to make some statistics.
-
whitequark almost 14 yearsIsn't it system-wide only?
-
Dennis Williamson almost 14 years@whitequark: It depends on the version of
sar
. Some versions usesar -x PID
others usepidstat -p PID
. -
keponk almost 14 yearsthatas perfect, i had already made a long script to clean all the data from a top batch output, but the is way simple better and efficint i aprreciate t a lot, thanks!
-
keponk almost 14 yearsat the end i kept using my script, since ps gives me the load average which is normally same as CPU% but is not a rule, more info of this in a quick google search
-
saji89 over 11 yearsWhilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
-
Will Sewell about 10 yearsThe %CPU here is not the % used over the interval, but the average percent over the entire running time of the program. I believe this is misleading.