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.


The standard ps is enough.

while true; do ps o pcpu,rsz -p $pid | tail -n1 >>usage.log; sleep $interval; done


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.

  • 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
    Jun 9 '10 at 14:55
  • 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
    Jun 15 '10 at 15:16
  • 1
    The %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. Apr 1 '14 at 14:55

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'

I would suggest sadc / sar.

  • Isn't it system-wide only?
    – whitequark
    Jun 8 '10 at 1:05
  • @whitequark: It depends on the version of sar. Some versions use sar -x PID others use pidstat -p PID. Jun 8 '10 at 1:36
  • 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
    Jan 3 '13 at 8:35

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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.