Automate Your Life With Cron

Example 2: Checking System Temperature Scenario

We re-purposed an older desktop computer as a Minecraft server. Because it is older hardware and will have the potential to be under heavy load, we want to keep an eye on the system temperatures to ensure it does not overheat. If a computer becomes too hot, there is a possibility the hardware can be permanently damaged leading to function and data loss. In addition, computers run slower under high heat and we would like to avoid that as well.


We can solve this problem by periodically checking the system temperatures to ensure they are at acceptable levels. We want to check it often to get a good idea how warm it gets at different times of day but we don't want to constantly check it manually. We can create a Cron job to run a script every few minutes to check the temperatures automatically and alert us if they are getting out of hand. First, let's set up a Cron job to run the script every five minutes.

*/5 * * * *  root  /home/ben/

That was pretty simple. Let's take a quick look at the script it will be running. Please note, this script is specifically written for my test server. The sensors command will likely have different output for your specific system and the number of hard drives in your system may be different.

  1. #!/bin/bash
  3. cpu_min=5
  4. cpu_max=60
  5. vid_min=5
  6. vid_max=80
  7. hdd_min=5
  8. hdd_max=60
  10. cpu0=`sensors | grep -e 'Core 0' | awk '{print $3}' | sed 's/[^0-9\.]//g'`
  11. cpu1=`sensors | grep -e 'Core 1' | awk '{print $3}' | sed 's/[^0-9\.]//g'`
  12. vid0=`sensors | grep -e 'temp1' | awk '{print $2}' | sed 's/[^0-9\.]//g'`
  13. hdd0=`hddtemp /dev/sda | awk '{print $4}' | sed 's/[^0-9\.]//g'`
  14. hdd1=`hddtemp /dev/sdb | awk '{print $4}' | sed 's/[^0-9\.]//g'`
  16. log_file="/var/log/temps.log"
  18. # Create an empty array to store warning messages
  19. declare -a messages
  21. # Print the temperatures to a log file
  22. printf "$(date +'%m/%d/%Y %r') [INFO]: CPU 0: %.1f°C\tCPU 1: %.1f°C\tHDD 0: %.1f°C\tHDD 1: %.1f°C\tVID 0: %.1f°C\n" $cpu0 $cpu1 $hdd0 $hdd1 $vid0 >> $log_file
  24. # Check if the CPU is overheating, if so add a message
  25. if [ $(echo "$cpu0 > $cpu_max" | bc) -eq 1 ] || [ $(echo "$cpu1 > $cpu_max" | bc) -eq 1 ]
  26. then
  27. messages[${#messages[@]}]=$( printf "Bad CPU temp\tCPU 0: %.1f°C\tCPU 1: %.1f°C\tCPU Max: %.1f°C" $cpu0 $cpu1 $cpu_max )
  28. fi
  30. # Check if the Hard Drives are overheating, if so add a message
  31. if [ $(echo "$hdd0 > $hdd_max" | bc) -eq 1 ] || [ $(echo "$hdd1 > $hdd_max" | bc) -eq 1 ]
  32. then
  33. messages[${#messages[@]}]=$( printf "Bad HDD temp\tHDD 0: %.1f°C\tHDD 1: %.1f°C\tHDD Max: %.1f°C" $hdd0 $hdd1 $hdd_max )
  34. fi
  36. # Check if the Video Card is overheating, if so add a message
  37. if [ $(echo "$vid0 > $vid_max" | bc) -eq 1 ]
  38. then
  39. messages[${#messages[@]}]=$( printf "Bad VID temp\tVID 0: %.1f°C\t\t\tVID Max: %.1f°C" $vid0 $vid_max )
  40. fi
  42. # If any of the temperatures exceeded their limits, email and add the warning to the log file
  43. if [ ${#messages[@]} -gt 0 ]
  44. then
  45. # Email me about the situation
  46. /usr/bin/mail -s "Server Overheating on $(date +'%m/%d/%Y %r')" "<Your Email Here>" << END_MAIL
  47. ${messages[@]}
  48. END_MAIL
  50. # Add each warning to the log file
  51. for message in "${messages[@]}"
  52. do
  53. printf "$(date +'%m/%d/%Y %r') [WARNING]: $message\n" >> $log_file
  54. done
  55. fi

We know from the Using Cron page that the date and time elements are telling Cron to schedule this job every minute in increments of five (in other words, every five minutes). Then, the second element tells Cron to run the command as root. This is required because the hddtemp utility in Linux requires root privileges. Finally, the last element tells Cron the command we'd like to run.

The bash script is called each time to check the temperature of different sensors in our machine. If the sensors read a value higher than our maximum limits, an email is sent to us warning of the issue. Additionally, regardless of limits, the sensor temperatures are recorded each time the script is run. This way, we can see how warm our system is throughout any given day. Thanks to Cron, we don't have to sit around gathering sensor output all day. We can look at the log file after a day of running and see if the system requires additional cooling. Thanks again Cron!

Let's move on to one more example now.