Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am looking for a way to write the CPU utilization into a text file every 10 seconds.

How might I go about setting this up? I'm running Ubuntu 12.04.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

CPU utilisation is easy:

From the command line:
while ( sleep 10 ) ; do cat /proc/loadavg >> mylogfile ; done

The sleep command will sleep 10 seconds and than return with the return value 0 (aka success). We abuse that to get a compact while( true ) sleep 10.

/proc/loadavg contains the load avarages of now, over the last 5 minutes, and over the last 15 minutes. If you are logging every 10 seconds then you are only interested in the first value.

Or in a script (using bash).

#!/bin/sh
# Using /bin/sh which is guaranteed to be present on any posix system.
# If you want to add shell specific parts in the script than replace this.
# E.g. if you want to use bash specific stuff then change it to:
# #!/usr/bin/env bash
# Make sure that the shebang is on the first line of the script (no comments above it!)

# While true, pause 10 seconds, then append information to Mylogfile
#
while ( sleep 10 ) ; do cat /proc/loadavg >> mylogfile ; done


We can add a cat /proc/meminfo to the information we append to the log file. /proc/meminfo is quite extensive and it will log a lot. If you only want to filter on specific memory information then please add that to the post.

The simplest form of that would result in:
while (sleep 10) ; do cat /proc/loadavg /proc/meminfo >> mylogfile ; done).

share|improve this answer
    
Loadavg doesnt give the cpu utilization. It gives the average job queue length. –  Sahil Singh Jan 21 at 18:54

If you run atop as a daemon, it will log a huge amount of system state data: CPU usage, process list, disk I/O, memory usage and more. You can then step through the data with atop -r [filename].

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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