Free Core Temperature Logger for your mac
I have a late 2011 mac that nowadays gets really hot, especially on a warm day. I thought about some potential cooling methods/hacks I could use, but in order to know if they work or not I need to have an “average” temperature during normal operation averaged over a 10 day period that I can compare against whatever hack I end up doing. It seems there are a few software that tells you your mac’s temperate like Temp Monitor or Monit which are easy to use but you have to pay and they do not provide a log.
So…upon looking for free alternatives I found this free alternative available on github.
To download it, click on the green button labelled “clone or download” , right click to copy the https link and then fire up your terminal. In your home directory do
git clone https://github.com/lavoiesl/osx-cpu-temp.git
This will download the program. Then do cd in the directory you just downloaded and in your terminal type
make
To get a temperature reading now you just do the command
./osx-cpu-tem
Write a script that will log the temperature in a .txt file
In your terminal do
touch my_temp_loger.sh
This creates an empty file that will contain the commands for your logging script. Now open this file using any text editor and copy and paste the commands below and save.
#!/bin/bash
temp=$(./osx-cpu-temp)
echo $(stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S") $temp >> /full/path/to/your/log/file/temp_log.txt
Lets look at the commands below more carefully now
#!/bin/bash
This is how a bash script should start, its the path of your bash interpreter.
temp=$(./osx-cpu-temp)
Here we save the output of the temperature script (we downloaded before) to the variable called temp.
echo $(stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S") $temp >> /Users/tofarisandreas/my_code/software_important/mac_temp_monitor/osx-cpu-temp-master/temp_log.txt
Here we use echo to amend two variables together. The first will be the Date/Time variable and the second the temp variable. So that every line in our temperature logger file looks like this:
2018-06-26 10:28:00 61.8°C
The stat command used just specifies to date format we want to use, in this case we use YEAR MONTH DATE HOUR:MINUTE:SECONDS
$(stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S")
The » is used to AMEND the echo output to the last line of the text file specified after the » . This means that every time the script runs it will write a new line with date and temperate to the same “logger file”.
Save you my_temp_loger.sh script, go back to the directory you have this script. Before it runs you need to make this script executable, to do this do:
chmod +x my_temp_loger.sh
Your can now run this script by doing
./my_temp_loger.sh
You should now see the date and temp in your logger file.
Running your script automatically using crontab
In order to do this you need to make a few “adjustments” on your script otherwise cron will will just register date.
In your script at the end write
echo $PATH
Copy this path output and then paste it under bin bash in your script.
#!/bin/bash
export PATH="here/you/copy/the/output/of/the/echo_path/above"
Also because we need to specify absolute paths now before the temp variable do a cd to the directory you have your temperature logger file like below
cd /you/temperature/directory
temp=$(./osx-cpu-temp)
Leave the rest unchanged.
Save your new script, and will will carry on with setting up the cron tab.
Setting up crontab
In your terminal do:
env EDITOR=nano crontab -e
This opens up crontab jobs, and copy and paste the line below
*/30 9-17 * * 1-5 /absolute/path/to/your/temp/logger/script.sh
This will run the your bash script (specified with absolute path) every hour from 9am to 5pm Monday to Friday.In the command above:
- First option is the minutes in every hour (0-59), zero in this case
- Second option in the hours (0-23) you want the crontab to run, this can take a range of hours as well and /2 means to run every 2 hours in that range
- Third option is the days of the month (1-31), * means all day on the month.
- Forth option is the months of a year (1-12), here the * means any month
- Fifth option is the range for the days in a weak, (1-5) means Monday to Friday
Example for cron to run every 10 min
*/10 * * * * /scripts/monitor.sh
ctrl+x to exit nano, it will ask you to save type Y to confirm.
Your output .txt file would look like this
DATE TIME TEMP(C)
2018-06-26 13:00:00 60.8°C
2018-06-26 13:30:00 73.9°C
The data gathered will be used for comparison purposes later on.