LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices

Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Ugly script to sleep "accordingly" to CPU temperature, on loops

Posted 03-28-2014 at 08:40 PM by the dsc

Sometimes you have a script or one-liner loop thing that may put quite a heavy load on the CPU, that can be at least bothersome due to the noise of the fan, or somewhat worrisome if the temperature approaches or goes beyond what the processor specifications recommend, and yet the system doesn't do anything by itself to avoid it.

One could simply add some arbitrary, constant sleep time within the loop, but that will likely make the whole thing slower than it needs to be. If within the loop there's another script that just sleeps accordingly to CPU temperature or fan's RPM, you have the best of both worlds, sort of. Potentially/almost necessarily faster than you'd have with an arbitrary sleep time, but without the overheating and noise that could come with no sleep time at all.

However ugly it can be, it does the work.


Code:
#!/bin/bash


coolsub()
{

temp=$(sensors | grep "Core 0" | sed 's|.*||;s|.*+||;s|\..*||') 

case $temp in

3[0-5])
cool="0"
;;

3[6-9])
cool="0"
;;


4[0-2])
cool=0.15
;;

4[3-5])
cool=0.35
;;

4[6-7])
cool=0.65
;;

4[8-9])
cool=1.25
;;

5[0-2])
cool=1.8
;;

5[3-5])
cool=2.3
;;

5[6-7])
cool=2.7
;;


5[8-9])
cool=3.5
;;

6[0-4])
cool=7

;;

6[5-9]|7[0-9])


echo -e "cooling...$temp\c"
until [ $temp -lt 51 ] ; do

echo -e "...$temp\c"
sleep 0.7

temp=$(sensors | grep "Core 0" | sed 's|.*||;s|.*+||;s|\..*||') 

done

echo


cool=3
;;

8[0-9])
cool=80
;;

9[0-9])

cool=160
;;

1[0-9][0-9] )

cool=200

;;

esac


sleep $cool

}

coolsub

I don't know for sure why it's just a function that calls itself. Perhaps because it was originally just a function that I came up with to use within a script that had a loop, then I thought I could use it in other instances, and that was the laziest way to achieve it.

I may have accidentally erased something important when I was cleaning useless comments, but I guess I did not.


I have no idea how the whole part that gets the temperature will generalize for other hardware and/or OS specifications, and I can't help you with that.


If it enters the 65-79 degrees Celsius range, it will go into another loop from which it only exits when the temperature gets lower than 51C. Just an arbitrary choice based guesswork plus my CPU's temperature recomendations.
Posted in Uncategorized
Views 210 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 10:14 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration