Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 7 down vote accepted

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.

share|improve this answer
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
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. – Will Sewell 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'
share|improve this answer

I would suggest sadc / sar.

share|improve this answer
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. – Dennis Williamson 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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .