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

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.


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


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