Log CPU/Load Usage On Your Mac 4

Tip

Share
Log CPU/Load Usage On Your Mac

Hey

This trick I have been wondering about for a while. The idea behind this is that it logs your load usage of your computer. You can then graph, using a program like Excel, this data. You can use this data to see how your computers load is running over a period of time. Like many methods in Terminal there are hundreds of ways in which you could do this, how many ways can you paint a picture? This tutorial, although a bit complicated, goes into a lot of different subjects. Most of them won’t be talked about in a lot of detail. There is a lot of pages on the web which goes into more detail on each of the different areas covered in this post, a quick search will reveal them. This post has taken me a long time to prepare, due to a lot of little problems a long the way, it has taken me a couple of hours to debug a little problem. You will probably encounter them too. In the comments if you have any questions I will try and answer them as well as possible.

The first step is to create the script to run our little login program. The idea behind this script is to take the load value from uptime, and copy it into a text file for us to use later. Open up your favourite text editor and type the following. Copy and pasting may cause errors. Change /Users/James/ to a path suitable to you.

#!/bin/sh
echo `date`, `uptime | cut -f4- -d: | awk '{ print $1 }'` > /Users/James/file1.csv;
cp /Users/James/cpu.csv /Users/James/cpu2.csv;
cat /Users/James/cpu2.csv/Users/James/file1.csv > /Users/James/cpu.csv;

A quick run down to help you understand what is happening in this script. First you will have to make two blank files called cpu.csv and file1.csv. Cpu.csv is the file with all the values of you load. You use this file to plot your graph. File1.csv is a temporary file which holds your data. This is needed due to the way Terminal works, as explained later.

#!/bin/sh

This tells the cron program what language is being used in the script.

echo `date`, `uptime | cut -f4- -d: | awk ‘{ print $1 }’` > /Users/James/file1.csv;

The business end of the script. This takes the current date and the minute uptime load value and puts it into file1.txt. In your script you will have to change the path to your folder and create file1.csv.

cp /Users/James/cpu.csv /Users/James/cpu2.csv;

This copies the file with all the data (cpu.csv) and names a new file called cpu2.csv.

cat /Users/James/cpu2.csv /Users/James/file1.csv > /Users/James/cpu.csv;

This final step merges the copied file with the file with the current value (file1.csv) and outputs it into cpu.csv.

The reason you do this is simple. You can’t insert directly information into a file without overwriting it, line 2 of the code. As a result you create a temporary file to insert this data. You then merge the temporary file with the final file. But you don’t do: file1.csv + cpu.csv = cpu.csv, as this can cause an unstoppable loop. Generally problems. Its hard to follow. But these four lines work.

The next step is to make this file executable. Save your file containing the code with a suitable file name. Something like cpuload.sh. You need the sh bit. In Terminal type the following.

chmod +x /path/to/cpuload.sh

This will make it executable. Test the script out by typing, in Terminal.

/path/to/cpuload.sh

If all goes well cpu.csv should display a value similar to this:

Mon Aug 18 22:37:00 BST 2008, 0.24

The comma is used so in Excel it places the date in one cell and the load in another cell. We can easily create a graph this way.

The final step is to activate cron. Cron is a system which can automatically run scripts and tasks for you. We want the script we have created to run every minute. In Terminal again type:

crontab -e

A page with tilda symbols should appear. This is a vim editor. Doesn’t look like it. Press “a” and then type the following:

* * * * *  /path/to/cpuload.sh

The stars are the time settings. For more infromation Google cron help and it will tell you in more detail. The stars mean in this case to run every minute of every hour, or every day, of every week of every month. Basically every minute.

To make this active press escape. Type without quotes “:wq!” and hit enter. Then type the following to see if the cron tab works.

crontab -l

That is basically it. You let it run for about ten minutes. It should generate some time and load values. After a while you can then plot these in a graph. For example these are the load values as I wrote this post.

Hopefully you have understood how the steps are completed and you have got it working. If you do run into problems please leave a comment below and I will try and help you. Surprisingly it has taken me a long time to get the script to work. Small commands just didn’t want to work as I hoped. You will agree though, this script is small. Although it doesn’t take a while to implement to does its job well. If you want to take this script further please do. You can leave any additions below or email me the scripts. I can’t wait to run this script over a day to see exactly what is happening and how the load fluctuates up and down.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips

4 Responses to “Log CPU/Load Usage On Your Mac”

  1. 1

    Hmmm…no load values

    Fri Aug 22 18:20:00 CDT 2008,
    Fri Aug 22 18:21:00 CDT 2008,
    Fri Aug 22 18:22:00 CDT 2008,

    #!/bin/sh
    echo `date`, `uptime | cut -d : -f 3 | awk ‘{ print $1 }’` > /Users/rgreene/file1.csv;
    cp /Users/rgreene/cpu.csv /Users/rgreene/cpu2.csv;
    cat /Users/rgreene/cpu2.csv /Users/rgreene/file1.csv > /Users/rgreene/cpu.csv;

    seems happier

    Other than that, thanks! Useful info.

    Comment By Bob G on August 23rd, at 12:36 am

  2. 2

    Oops!…I lied.

    Not sure why yet, but now the original script works. Guess I need to read up on cut.

    Comment By Bob G on August 23rd, at 1:28 am

  3. 3

    Its weird that. I have noticed it to but only very so often. I just thought it was a problem with my computer.

    Comment By admin on August 23rd, at 10:33 am

  4. 4

    you can do the same using double “>>” this appends new data to the end of your data file. and if it does’t exist it creates the file.

    #!/bin/sh
    echo `date`, `uptime | cut -f4- -d: | awk ‘{ print $1 }’` >> /Users/yourname/cpu.csv

    Comment By Atila on February 24th, at 4:28 pm