Monday, October 29, 2012

Linux: Cool your CPU temperature with frequency throttling

Last night I went to sleep while running a heavy program that used 100% of my Intel i7 multicore processor. When I woke up in the morning, I found my laptop powered off. A look through the /var/log/syslog confirmed that it had shut off due to over heating.

So to control temperature, I wrote a script to throttle cpu frequency. This script must be run with root privileges (sudo) to work. For a desired max temperature of 80 degrees Celsius, use a command like this:
sudo ./temp_throttle.sh 80

This can also result in longer battery life on laptops and portable devices.


Download link: temperature throttling script for Linux - temp_throttle.sh
New User Resources:

Download link: temperature throttling script for Linux - temp_throttle.sh

Project source page: https://github.com/Sepero/temp-throttle



NOTICE: This script may not be effective if your system over heating is due to factors other than your CPU (ie. graphics, harddrive, etc.). This script will only throttle your CPU frequency based on your systems reported temperature. Opening and cleaning dust from a computer can often dramatically help cool it also.

If this script helps you in any way, please pay me with a message of thanks, and share this page with others.


Comment or leave feedback sepero 111 @ gmx . com

Keywords:
overheating cpu scaling temperature control Linux

34 comments:

  1. Hi, nice script! umm, my /sys/class/hwmon/hwmon0/temp1_input
    always reports 42 C, so I changed that line to
    TEMP=$(cat /sys/class/hwmon/hwmon1/device/temp1_input)
    that reports the temp the same as my gkrellm lm_sensors CPU temp monitor.

    thx!

    ReplyDelete
    Replies
    1. Thanks for the info, Willem. I've updated the code to hopefully accommodate more people.

      Delete
    2. I am running Fedora 19 and I had to change that path to
      /sys/class/hwmon/hwmon1/temp1_input
      Please note that the device directory does not have temp1_input. This took a little while for me to figure out.

      After I changed this my machine runs beautifully. Also, Pedro's method for running the script as root worked wonders.

      This makes my machine usable again. I also have an i7 but my biggest has always been that it will burn up to the critical temperature and shut off.

      Thanks so much!

      Delete
  2. Finally! I've found you!
    I published your script at http://forum.rosalab.ru/viewtopic.php?f=39&t=1645 I wanted to ask your permission, but lost your blog :) Sorry.

    And more.
    Some people have even more problems, their kernel module for cpu frequency controlling not loading automatically. I have seen it on arch, mandriva/mageia/rosa, fedora, opensuse (hey! they all rpm distros!) and if you work on laptop without frequency control - huh, it gets hot!
    I don't want to dig deeper into why is this happening, I just wrote autostart script for starting your script with automatic detection if selected module loaded and loading module with selected governor.
    Script was written for RosaLinux/Mageia/Mandriva/Fedora, but I think you can easilly adopt it for Ubuntu or anything else (only change path to /sys/.../cpufreq folder and change comand cpupower to cpufreq-set (or write your own :) )

    ReplyDelete
  3. Hi Russian friend, thank you for sharing. I'm glad it worked out great for you. :)

    ReplyDelete
  4. Thank you very much Sepero, this script works very well in Fedora 18, no more shutdowns while watching Youtube videos...

    Just want to add the simple instructions to run the script on boot as root in Fedora 18:

    Create rc.local:

    # touch /etc/rc.d/rc.local

    Make it executable:

    # chmod +x /etc/rc.d/rc.local

    Open rc.local and add the following text:

    #!/bin/bash

    /root/temp_throttle.sh 98

    Change the last line with your path to the script and desired temperature, and don't forget to make the script executable too.

    Check if rc-local.service is running:

    # systemctl status rc-local.service

    Reboot and check if all went well by running again the above command, if all went well you should see something like this:

    # systemctl status rc-local.service
    rc-local.service - /etc/rc.d/rc.local Compatibility
    Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static)
    Active: activating (start) since Seg 2001-01-01 06:46:14 WET; 12 years 3 months ago
    Control: 1003 (rc.local)
    CGroup: name=systemd:/system/rc-local.service
    ├─ 1003 /bin/bash /etc/rc.d/rc.local start
    ├─ 1023 /bin/bash /root/temp_throttle.sh 98
    └─25269 sleep 3

    ReplyDelete
  5. Great command. You can also use accurate temperature measurement devices and see how they work.

    ReplyDelete
  6. Works great on Ubuntu 12.10. Thanks!

    ReplyDelete
  7. I`ve got an error:

    $ sh temp_throttle.sh 80

    temp_throttle.sh: 23: temp_throttle.sh: Syntax error: "(" unexpected
    ipeacocks@homepc:~/Downloads/cpu/temp-throttle-master$ bash temp_throttle.sh 80
    Author: Sepero (sepero 111 @ gmail . com)
    Remote Python developer and Linux administrator for hire.
    URL: http://github.com/Sepero/temp-throttle/

    Number of CPU cores detected: 4

    cat: /sys/class/thermal/thermal_zone0/temp: No such file or directory
    temp_throttle.sh: line 105: [: -gt: unary operator expected
    temp_throttle.sh: line 107: [: -le: unary operator expected

    ReplyDelete
    Replies
    1. Hi Ipeacocks, it doesn't run because you weren't using bash shell. I have made an update to the script so that it should now run with busybox shell also. Hopefully that helps. Cheers.

      Delete
  8. This is fantastic. My ASUS laptop temp was breaking 80c running WINE, and now it doesn't top 65. When under load it stills runs a good 5-10c hotter than Windows 7, but it's much improved over the 10-20c difference I was seeing previously, both idling and under load. Thank you so much for this script!

    ReplyDelete
  9. This is great!.. My HP laptop was always shutting down due to overheating. Now its stays below 80c often running at 75c while it usually run at 95-103c

    ReplyDelete
  10. Hi Sepero I'm new in this, I run the script but I,ve git this error

    noelie@noelie-Aspire-5315:/tmp/temp-throttle-master$ sh temp_throttle.sh 80
    Author: Sepero 2013 (sepero 111 @ gmx . com)
    URL: http://github.com/Sepero/temp-throttle/

    -e Number of CPU cores detected: 1


    Error: Could not determine available cpu frequencies
    noelie@noelie-Aspire-5315:/tmp/temp-throttle-master$

    ReplyDelete
    Replies
    1. @madrisieraa, You have a technical problem that cannot be easily answered in the comment section of a blog. For further help, please contact me via email or on post an issue on github.com. Thank you

      Delete
  11. Nice script. Very instructive. Actually took it only as a blueprint to throttle my CPU to a fixed max frequency. I have a Gaming Laptop with aggressive fan control. Throttling the CPU keeps the fan from speeding up during load peaks making it much more suitable for work related tasks (being still fast enough).

    ReplyDelete
  12. Finally I fixed my laptop overheating problem with your script. I already was thinking of switching to windows. There are a lot of people struggling with overheating issue, just google "overheating linux" and you will see how many result will get back. We most share this script with the people who are struggling with this issue. THANKS ALOT Sepero

    ReplyDelete
  13. thanks for sharing

    my laptop used to die when playing youtube videos for extended time, after showing kernel message like:

    CPU1: Core temperature above threshold, cpu clock throttled (total events = 131)

    I also think I found a method to choose which files to monitor: the computer halts when CPUs overheat, but which files do I have to watch to check for that?

    to discover it, run the following command in a terminal along with your favourite overheating application:

    while true; do sensors |grep Core; find /sys/devices/virtual/thermal/ -name temp|xargs -n1 grep -H .|grep -v :0; sleep 5;done


    my output is like:

    Core 0: +63.0°C (high = +80.0°C, crit = +90.0°C)
    Core 2: +61.0°C (high = +80.0°C, crit = +90.0°C)
    /sys/devices/virtual/thermal/thermal_zone0/temp:68000
    /sys/devices/virtual/thermal/thermal_zone3/temp:50000
    /sys/devices/virtual/thermal/thermal_zone5/temp:24900
    /sys/devices/virtual/thermal/thermal_zone7/temp:62000
    /sys/devices/virtual/thermal/thermal_zone8/temp:65000
    /sys/devices/virtual/thermal/thermal_zone9/temp:78000

    first lines are the CPU physical core(s) temperatures, other are temperature monitoring files

    observe it from some time as the temperature changes, and look for the files that follow the sensors output. for some reason probably some data fitlering, I never get an exact match, but I found out that in my case the sensors are thermal_zone7 and thermal_zone8. edit TEMPERATURE_FILES accordingly, leave only the core temperatures.

    you might also want to change the get_temp functions so that it finds the core max of core temperatures:

    get_temp () {
    TEMP=-1
    # Get the system temperature.
    for FILE in $TEMPERATURE_FILES; do
    TEMP_i=$(cat $TEMP_FILE)
    if [ $TEMP_i -gt $TEMP ]; then
    TEMP=$TEMP_i
    fi
    done
    }

    ReplyDelete
    Replies
    1. HCE, it is difficult to discover the precise location of CPU temperature due to different machines reporting them differently. Some systems report very differently than what is common for most, and I would guess that your system falls in this category.

      You have suggested that I "edit TEMPERATURE_FILES accordingly, leave only the core temperatures". I understand that you suggest I edit the variable TEMPERATURE_FILES, but I'm not quite sure I understand how you think I should edit it. Perhaps you could give me an example of how you think it should look.

      Also, you suggest that the script should test for the highest temperature among all cores. On a normal system, all cores tend to stay within the same temperature range of each other. Though, I think you have an interesting idea for me to consider.

      Thank you for your great feedback

      Delete
  14. What if I end the program while in the middle of the "throttle" ? the "unthrottle" will not happen thus my cpu freq will be lower and if the next throttle happens (by executing the program) it will be throttled DOUBLE TIME?

    ReplyDelete
    Replies
    1. If you end the program, your CPU will remain at the last state throttled. Your CPU will cannot be throttled "double time".

      If you want to restore your CPU to full throttle, stop the program and run it again with a very high temperature (like 150). After 10 seconds, you can end the program again.

      Delete
  15. Hey this is cool, thanks! But, sometimes it allows the CPU to go beyond the set temp before throttling it back. I checked that with sensors while running the script. Do you have any idea why?

    ReplyDelete
    Replies
    1. The script checks for temperature updates every 3 seconds (this interval can be changed at the end of the script). When temperature has been exceeded, it will throttle down the CPU incrementally until a desirable temperature has been reached.

      If the CPU appears to be higher than expected, you may be reading from different temperature sensors than the script is reading from. Cheers

      Delete
  16. I have a macbook air 2008, but it does not seem to be monitoring core tmp any help appreciated gwheadon@live.co.uk email

    ReplyDelete
    Replies
    1. This script is only designed to work with Bash shell and the Linux kernel

      Delete
  17. Thank you, man. You have solved my problem. I was thinking in the purchase of a new laptop. Thanks again :)

    ReplyDelete
  18. Hi,

    For me the script gives this error:

    robert@robert-desktop:~$ sudo ./temp_throttle.sh 80
    [sudo] password for robert:
    Author: Sepero 2016 (sepero 111 @ gmx . com)
    URL: http://github.com/Sepero/temp-throttle/

    Number of CPU cores detected: 2


    Error: Could not determine available cpu frequencies
    robert@robert-desktop:~$

    Any idea what I can do?

    Thanks

    ReplyDelete
  19. Hi Mr.Sepero. I'm a new guy who just got here. I ran the script. Does this means every time i login, i must run the script or I just run it once?. Please sorry if my question is dump.

    ReplyDelete
  20. Do i need to let the script run till infinity, or just stop at some time?

    ReplyDelete
    Replies
    1. The script must be run at every boot. This can be automated with startup scripts. And yes, the script will run infinitely.

      Delete
  21. Ran the script and set the temp and it failed with:

    Error: Could not determine available cpu frequencies

    ReplyDelete
  22. This is pure bliss!!!
    (a note to those with scripts not working)

    Firstly use the script from the GitHub link not the direct download, that file doesn't throttle anything

    In the GitHub script, edit the TEMPERATURE_FILES paths, should be line 73, with the paths to you input temps, or read etc. I used 2 line, since my laptop reads 2 cores only.

    And youre done... sudo it, temp it, however you like, I ended removing the if statement to check for a varible and set MAX_TEMP=70 permanently (in the file of course), so it could run at start up without errors

    ReplyDelete
  23. Awesome! Overheating has been a persistent problem on my laptop. I have tried fan control and cpulimit, but those approaches were too difficult and unreliable. temp_throttle.sh works beautifully! Thank you!

    ReplyDelete
  24. Thank you!
    I have 2 typical computing usages that have been optimized thanks to your script and website (SEO sucks for low popularity sites, it's like my 15th search on gg/ddg)

    1) my friend can run his phd scripts with heavier load on my Oc'd desktop, because scripts may run at 100% on cores which is not good for stability and unusual peak
    temperatures

    2) My shitty cooled msi ge72 "best bang for buck 3 heatpipes with nuclear i3" can now run games without me having to be concerned on temps over watch sensors or i7Z commands. (the cpu does output way more watts than the cooler ouput, but nby really care except geeks)

    Linux mint runs 10°C colder than w10 on my laptop. Since I've left W8.1, I've started learning using linux as a poweruser, I have striclty no control over w10pro sometimes, it can download stuff even with aggressive policies and disabled update service.

    So now I have equal control on cpu management on both w10 and linux; thanks again, starred your github

    ReplyDelete
  25. I agree with everybody here. This is excellent, man. Thank you!

    ReplyDelete