{"id":4727,"date":"2016-07-10T21:31:44","date_gmt":"2016-07-10T20:31:44","guid":{"rendered":"http:\/\/www.celesteh.com\/blog\/?p=4727"},"modified":"2016-07-11T11:16:23","modified_gmt":"2016-07-11T10:16:23","slug":"raspberry-pi-on-a-hot-day","status":"publish","type":"post","link":"https:\/\/www.celesteh.com\/blog\/2016\/07\/10\/raspberry-pi-on-a-hot-day\/","title":{"rendered":"Raspberry Pi on a hot day"},"content":{"rendered":"<h3>Edit<\/h3>\n<p><em>Whatever is causing my Pi to crash corresponds with a spike in heat, but I now suspect it&#8217;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&#8217;s own. I&#8217;m leaving this up because parts of it are interesting, like suspending processes, writing functions, etc<\/em><\/p>\n<h3>A specific case<\/h3>\n<p>Let&#8217;s say you want to build the latest version of inkscape on your raspberry pi computer. And let&#8217;s say you want to install it via dpkg. You may find it crashes from overheating.<\/p>\n<p>Fortunately, I&#8217;ve got a <a href=\"https:\/\/github.com\/celesteh\/Immrama\/blob\/master\/pi_build_inkscape.sh\">script<\/a> for you.<\/p>\n<p>Before running the <a href=\"https:\/\/github.com\/celesteh\/Immrama\/blob\/master\/pi_build_inkscape.sh\">script<\/a>, do the following:<\/p>\n<ul>\n<li><code>sudo nano \/etc\/apt\/sources.list<\/code><\/li>\n<ul>\n<li>Uncomment the line with deb-src in it<\/li>\n<\/ul>\n<li><code>sudo apt-get update && sudo nice apt-get upgrade -y<\/code><\/li>\n<li><code>sudo apt-get build-dep inkscape -y<\/code><\/li>\n<li><code>sudo apt-get install build-essential dpkg-dev fakeroot cmake bzr dh-make -y<\/code><\/li>\n<li><code>sudo apt-get remove inkscape<\/code><\/li>\n<li><code>sudo apt-get autoremove<\/code><\/li>\n<\/ul>\n<p>Then, when it&#8217;s done:<\/p>\n<p><code> .\/pi_build_inkscape.sh && cd ~Downloads\/ && sudo dpkg -i inkscape*.deb<\/code><\/p>\n<h3>The General Principle<\/h3>\n<p>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&#8217;t return values, so we&#8217;ll write a global variable.<\/p>\n<p><pre>\r\n<code>\r\n#!\/bin\/bash\r\n\r\ndeg=0\r\n\r\nfunction get_temp {\r\n  cpu=$(&lt;\/sys\/class\/thermal\/thermal_zone0\/temp)\r\n  deg=$((cpu\/1000))\r\n}\r\n\r\nget_temp\r\necho \"$deg c\"\r\n<\/code>\r\n<\/pre>\n<p>That&#8217;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&#8217;ll do some recursion.<\/p>\n<pre>\r\n<code>\r\nfunction pause_proc {\r\n  #renice -n 15 $1\r\n  kill -TSTP  $1\r\n  for i in `ps -ef| awk '$3 == '$1' { print $2 }'`\r\n  do\r\n    #echo \"pausing $i\"\r\n    pause_proc $i\r\n  done\r\n}\r\n\r\nfunction resume_proc {\r\n  kill -CONT $1\r\n  for i in `ps -ef| awk '$3 == '$1' { print $2 }'`\r\n  do\r\n    #echo \"restarting $i\"\r\n    resume_proc $i\r\n  done\r\n}\r\n<\/code>\r\n<\/pre>\n<p>Now we just need some way to track if a process is still running, and to sleep between temperature checks etc etc<\/p>\n<pre>\r\n<code>\r\nfunction run_proc {\r\n  \r\n  stopped=0\r\n  sleep=1\r\n\r\n  get_temp\r\n  echo \"$deg c \" `date +%X`\r\n\r\n  while kill -0 \"$1\"; do #run this loop while the process is running\r\n\r\n    get_temp\r\n\r\n    if [ $deg -ge 49 ] ; then # high temperature is 49\r\n      echo \"too hot! $deg c \" `date +%X`\r\n\r\n      pause_proc $1 # Do this every time in case we missed some last time\r\n      stopped=1\r\n      if [ $sleep -eq 5 ] ; then\r\n        sleep=10\r\n      elif [ $sleep -eq 10 ] ; then\r\n        sleep=15\r\n      elif [ $sleep -lt 5 ] ; then\r\n        sleep=5\r\n      fi\r\n    fi\r\n    if [ $stopped -eq 1 ] ;then\r\n      if [ $deg -le 47 ] #Ok temperature is 47\r\n        then\r\n        echo \"cool enough $deg c \" `date +%X`\r\n        resume_proc $1\r\n        stopped=0\r\n        sleep=1\r\n      fi\r\n    fi\r\n\r\n    sleep $sleep\r\n\r\n  done\r\n}\r\n<\/code>\r\n<\/pre>\n<p>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&#8217;s. If you don&#8217;t know what&#8217;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.<\/p>\n<p>So if we want to use this for some intense process in a script, we could put those functions at the top (under the <code>#!\/bin\/bash<\/code> ), and run our command like:<\/p>\n<pre><code>\r\nnice -n 15 some_command arg1 arg2 &\r\nrun_proc $!\r\n<\/code><\/pre>\n<p>Or if you want to make this a script you can run from the command line with your intense command:<\/p>\n<pre><code>\r\nnice -n 15  $@ &\r\nrun_proc $!\r\n<\/code><\/pre>\n<p>To add extra resilience to my poor pi, I&#8217;ve also put a heat sink on the CPU and, right now, it&#8217;s sat in a dry bowl which is sat in another bowl which is full of ice. It&#8217;s like one of those old Supercomputers, except probably faster.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Edit Whatever is causing my Pi to crash corresponds with a spike in heat, but I now suspect it&#8217;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&#8217;s own. I&#8217;m leaving this up because parts of it &hellip; <a href=\"https:\/\/www.celesteh.com\/blog\/2016\/07\/10\/raspberry-pi-on-a-hot-day\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Raspberry Pi on a hot day<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[1],"tags":[359,6],"class_list":["post-4727","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-inkscape","tag-raspberrypi"],"_links":{"self":[{"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/posts\/4727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/comments?post=4727"}],"version-history":[{"count":4,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/posts\/4727\/revisions"}],"predecessor-version":[{"id":4731,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/posts\/4727\/revisions\/4731"}],"wp:attachment":[{"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/media?parent=4727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/categories?post=4727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/tags?post=4727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}