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

14 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