Writing a CPU/RAM usage log over a period of time to file on CentOS

8

1

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.

keponk

Posted 2010-06-07T22:05:04.440

Reputation: 118

Answers

11

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.

whitequark

Posted 2010-06-07T22:05:04.440

Reputation: 14 146

thatas 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 – 2010-06-09T14:55:05.780

at 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 – keponk – 2010-06-15T15:16:02.157

1The %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. – Will Sewell – 2014-04-01T14:55:28.643

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'

Aalex Gabi

Posted 2010-06-07T22:05:04.440

Reputation: 180

1

I would suggest sadc / sar.

Paused until further notice.

Posted 2010-06-07T22:05:04.440

Reputation: 86 075

Isn't it system-wide only? – whitequark – 2010-06-08T01:05:27.953

@whitequark: It depends on the version of sar. Some versions use sar -x PID others use pidstat -p PID. – Paused until further notice. – 2010-06-08T01:36:18.483

Whilst 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.

– saji89 – 2013-01-03T08:35:49.410

0

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

Celik

Posted 2010-06-07T22:05:04.440

Reputation: 101

0

You're trying to observe how much cpu time a particular process is requiring.

I was going to suggest CPU stat tool for CentOS? but this gets it for the whole system, not just one process.

Any one process will show CPU time in the output of ps:

$ ps -ef | egrep blah
root     13988 11152  0 Dec16 ?        00:00:05 sshd: xxx [priv]
xxx      14024 13988  0 Dec16 ?        00:06:00 sshd: xxx@pts/0
xxx      14032 14024  0 Dec16 pts/0    07:00:00 -bash
root      1194   679  0 Apr24 ?        2-05:15:14 [kswapd0]
root      1195   679  0 Apr24 ?        2-06:35:49 [kswapd1]
  • 00:00:00 is hours/minutes/seconds of cpu. It will accumulate.
  • You'll need permissions to see this if the user is not you.
  • Once this value gets above a day the format changes; above it's 2 days. Your parsing routine will have to handle this.

At this point, parse the output with Python, work some date math on it, and you're golden.

Kevin J. Rice

Posted 2010-06-07T22:05:04.440

Reputation: 121