This is a radically updated version of the question. I have to update this because this question is flagged as duplicate of this one, while this has no valid answer anymore.
- Initial version of this question, which had the 11.04 tag:
I am new to scripting and linux, my comp gets too hot sometimes and I want to make a script to detect temp1 and if it's over 65 C, it must put it to sleep. I have a diffuculty at comparing values in the script, I couldn't manage to define figures correctly, would anybody fix it please? Here is my stab at it so far
#!/bin/bash
max=65
val=$ sensors | grep '^temp1:' | sed -e 's/.*: \+\([+-][0-9.]\+\)°C.*$/0\1/'
while true; do
if [[ "$val" > "$max" ]]; then
sudo /etc/acpi/sleep.sh force
sleep 1
else
sleep 10
fi
clear
sensors
done
The above got an answer with a script that according to the comments was at some point updated to work in 14.04:
#!/bin/bash
while true; do
val=$(sensors | awk '/temp1/ {print $2}')
max="+75.0"
if [[ "$val" > "$max" ]]; then
dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend
fi
sleep 10
clear
sensors
done
exit 0
As indicated in the linked question, the above script doesn't work in 16.04.
That question got an answer with a simplistically modified version of the script:
#!/bin/bash
while true; do
val=$(sensors | awk '/temp1/ {print $2}')
max="+75.0"
if [[ "$val" > "$max" ]]; then
systemctl suspend
fi
clear
sensors
done
exit 0
But while it does the job (the system goes to sleep when going above 75), it takes more CPU power than expected and pushes the temperature up while running up to 10 degrees celsius; this is more useful, more cooling when not running!
I don't know if the problem is with the initial 11.04 script or with the last change but this needs a fresh answer for 16.04.
I managed to make my own script.
For 16.04 (also here):
Introduction
In order to facilitate both pre-systemd and post-systemd switch users, I've written a script that will take appropriate method of suspending based on your os version number. Essentially it does the same thing that OP's updated script does, except it forces to suspend despite inhibitors with
-i
flag. There are multiple considerations and improvements that can be done to the script, but as for right now this version does 90% of the job.I tested it briefly on 16.04 LTS, works perfectly fine. In future I might rewrite this in Python just because I can or upon user request.
Script Source
Alternative approach
A while back, I found
pm-suspend
utility, frompm-utils
package. This program works regardless of the OS version. The small disadvantage is that it requires root access, but it is easy to get around of that inconvenience.What I personally would do is the following:
sudo apt-get install pm-utils
sudo visudo
and addyourusername ALL = NOPASSWD: /usr/sbin/pm-suspend
at the end of the file.sudo pm-suspend
instead of the dbus command.Suspending an Ubuntu machine in general
You've requested that answers draw upon credible sources. In fact, Ask Ubuntu does have canonical post about suspending from command-line: How can I suspend/hibernate from command line? Depending on the approach and level of permissions you want your script to have, there's multiple ways to skin a cat there. Some work better than others. In my answer I provided the dbus and systemctl approach, since those work with screen-locking as well. If you write to
/sys/class/power/state
, it won't lock the screen, although it is possible to get around that with some scripting magic. For now, I think, the better approach is to simply determine the OS version and select appropriate method as in my script, or usepm-suspend
alternative.On my system every time
sensors
is run there is a stutter in video streaming. Having this happen every 10 seconds or however often proposed script is run would drive me bat crazy. A better solution to suspend would be using Intel's thermald and Powerclamp to slow down the CPU in order to reduce heat. I've written this answer for another question (Stop cpu from overheating) and am copying here for convenience.Additionally the above script relies on
temp1
which is often corrupted on my Ubuntu 16.04 and onlytemp3
is 100% reliable which doesn't show up onsensors
. ie:and from
sensors
:This happens after suspend/resume. The REAL temperature is +58.0°C but is falsely reported as +27.8°C after resuming. So the heat protection would only work once to suspend and never work again until a reboot. So the system would hit critical (+106.0°C) at which point a hard power off is performed and data can be corrupted.
So here's my recommended solution to prevent overheating and utilizing CPU slow down rather than outright system suspend.
Slow down CPU to reduce heat
This works for Ubuntu 16.04+ with Intel Sandy Bridge and newer processors.
From (wiki.debian.org -thermald) is Debian's (used by Ubuntu) write up about thermald, a Linux daemon for cooling tablets and laptops. Once the system temperature reaches a certain threshold, the Linux daemon activates various cooling methods to cool the system.
Linux thermal daemon (thermald) monitors and controls temperature in laptops, tablets PC with the latest Intel sandy bridge and latest Intel CPU releases. Once the system temperature reaches a certain threshold, the Linux daemon activates various cooling methods to try to cool the system.
It operates in two modes:
Zero Configuration Mode
User defined configuration mode
How to install
Intel Powerclamp
Intel's Powerclamp driver is defined here (kernel.org - Intel Power Clamp.txt) and is part of thermald described above. A direct quote for Powerclamp from the link:
How do you know Powerclamp is running?
Powerclamp might only show itself once a year when your fan vents get too much dust & lint. So how do you know it's actually running in the background? Use:
And you should see a list similar to this:
If you see
intel_rapl
andintel_powerclamp
you know it's working and simply waiting temps to exceed 85C.Powerclamp in action displayed by Conky
Here is a screen shot when Powerclamp injects sleep cycles:
Normally on this system CPU clock speed is 2400 Mhz to 3400 Mhz when watching HTML5 video and 10 Chrome tabs open. Normally CPU utilization is about 9% to 12% across 8 CPUs. When things get too hot (86C) Powerclamp kicks in and this happens:
x
is from 0 to 7. For the first 8 CPUs.The Powerclamp driver runs until temps drop below 85C again. While the driver is running you might have split second pausing in your videos and possibly split second keyboard and mouse lag.
Disable Intel Turbo Boost
Back in the "cool old days" of Ubuntu 14.04 Intel Turbo Boost was broken so my processor speed fluctuated between 1200 Mhz and 2400 Mhz. After upgrade to Ubuntu 16.04 it would go up to 3400 Mhz (3.4 Ghz) because Turbo Boost was finally working. But it also raised the heat.
To disable Intel Turbo Boost use:
I have radically edited the above question in order to ask for updates for 16.04 and my answer below is therefore only an update to the the initial answer by Kenn, on which I posted a duplicate question and is also an answer under that.
As the error in 16.04 was
Error org.freedesktop.DBus.Error.UnknownMethod: No such method 'Suspend'
, after the comment made by Nick Sillito under my duplicate question linking to this answer, I have modified the script by replacing this entire part:with
As indicated in the comment made by wjandreea under the duplicate question:
sleep 10
or a similar value shouldn't be removed (as I initially did); without that line, the modified script will use more power because instead of running every 10 seconds, it will as fast as possible -- upwards of several dozen times per second.At this point the system goes to sleep when going above the level set in the line
As I want a higher value, 82, the script I use is: