Raspberry Pi on a hot day

Edit

Whatever is causing my Pi to crash corresponds with a spike in heat, but I now suspect it’s not caused by the heat, but rather the heat is generated by whatever is crashing it. The pi is meant to deal with heat problems on it’s own. I’m leaving this up because parts of it are interesting, like suspending processes, writing functions, etc

A specific case

Let’s say you want to build the latest version of inkscape on your raspberry pi computer. And let’s say you want to install it via dpkg. You may find it crashes from overheating.

Fortunately, I’ve got a script for you.

Before running the script, do the following:

  • sudo nano /etc/apt/sources.list
    • Uncomment the line with deb-src in it
  • sudo apt-get update && sudo nice apt-get upgrade -y
  • sudo apt-get build-dep inkscape -y
  • sudo apt-get install build-essential dpkg-dev fakeroot cmake bzr dh-make -y
  • sudo apt-get remove inkscape
  • sudo apt-get autoremove

Then, when it’s done:

./pi_build_inkscape.sh && cd ~Downloads/ && sudo dpkg -i inkscape*.deb

The General Principle

What you need for any overheating for of problem is the ability to check the temperature and then pause processes until it falls. First, how do we check the temperature? Bash functions can’t return values, so we’ll write a global variable.


#!/bin/bash

deg=0

function get_temp {
  cpu=$(</sys/class/thermal/thermal_zone0/temp)
  deg=$((cpu/1000))
}

get_temp
echo "$deg c"

That’s good. We can do some testing with $deg and put a process to sleep and wake it up again. But we also need to do subprocesses! We’ll do some recursion.


function pause_proc {
  #renice -n 15 $1
  kill -TSTP  $1
  for i in `ps -ef| awk '$3 == '$1' { print $2 }'`
  do
    #echo "pausing $i"
    pause_proc $i
  done
}

function resume_proc {
  kill -CONT $1
  for i in `ps -ef| awk '$3 == '$1' { print $2 }'`
  do
    #echo "restarting $i"
    resume_proc $i
  done
}

Now we just need some way to track if a process is still running, and to sleep between temperature checks etc etc


function run_proc {
  
  stopped=0
  sleep=1

  get_temp
  echo "$deg c " `date +%X`

  while kill -0 "$1"; do #run this loop while the process is running

    get_temp

    if [ $deg -ge 49 ] ; then # high temperature is 49
      echo "too hot! $deg c " `date +%X`

      pause_proc $1 # Do this every time in case we missed some last time
      stopped=1
      if [ $sleep -eq 5 ] ; then
        sleep=10
      elif [ $sleep -eq 10 ] ; then
        sleep=15
      elif [ $sleep -lt 5 ] ; then
        sleep=5
      fi
    fi
    if [ $stopped -eq 1 ] ;then
      if [ $deg -le 47 ] #Ok temperature is 47
        then
        echo "cool enough $deg c " `date +%X`
        resume_proc $1
        stopped=0
        sleep=1
      fi
    fi

    sleep $sleep

  done
}

I picked the temperatures of 47 and 49 based on when my pi tends to crash. theoretically, it should be ok up through the 60’s. If you don’t know what’s going on with your pi getting too warm, you can also set a loop in another window to print out the temperature in a loop.

So if we want to use this for some intense process in a script, we could put those functions at the top (under the #!/bin/bash ), and run our command like:


nice -n 15 some_command arg1 arg2 &
run_proc $!

Or if you want to make this a script you can run from the command line with your intense command:


nice -n 15  $@ &
run_proc $!

To add extra resilience to my poor pi, I’ve also put a heat sink on the CPU and, right now, it’s sat in a dry bowl which is sat in another bowl which is full of ice. It’s like one of those old Supercomputers, except probably faster.