Power Management only has the option of "Do Nothing" for critically low battery in 17.10
I'm using Ubuntu 17.10 with Gnome on an Asus laptop.
In the Power Settings interface, under the Power tab, there is an option at the bottom to set what to do in case the battery gets critically low.
However, when I click on the drop down to set that option, it only offers one option, which is "do nothing":
Currently, my laptop will run down to 0 battery power, or close to it, and then shut off with no warnings, no possibility of closing programs and shutting down gracefully.
I would like to have a warning when the battery is at 10% and hopefully some time to manually save and shut down programs before turning the computer off. How do I achieve this?
Edit: Here is the contents of my /etc/UPower/UPower.conf
file:
# Only the system vendor should modify this file, ordinary users
# should not have to change anything.
[UPower]
# Enable the Watts Up Pro device.
#
# The Watts Up Pro contains a generic FTDI USB device without a specific
# vendor and product ID. When we probe for WUP devices, we can cause
# the user to get a perplexing "Device or resource busy" error when
# attempting to use their non-WUP device.
#
# The generic FTDI device is known to also be used on:
#
# - Sparkfun FT232 breakout board
# - Parallax Propeller
#
# default=false
EnableWattsUpPro=false
# Don't poll the kernel for battery level changes.
#
# Some hardware will send us battery level changes through
# events, rather than us having to poll for it. This option
# allows disabling polling for hardware that sends out events.
#
# default=false
NoPollBatteries=false
# Do we ignore the lid state
#
# Some laptops are broken. The lid state is either inverted, or stuck
# on or off. We can't do much to fix these problems, but this is a way
# for users to make the laptop panel vanish, a state that might be used
# by a couple of user-space daemons. On Linux systems, see also
# logind.conf(5).
#
# default=false
IgnoreLid=false
# Policy for warnings and action based on battery levels
#
# Whether battery percentage based policy should be used. The default
# is to use the time left, change to true to use the percentage, which
# should work around broken firmwares. It is also more reliable than
# the time left (frantically saving all your files is going to use more
# battery than letting it rest for example).
# default=true
UsePercentageForPolicy=true
# When UsePercentageForPolicy is true, the levels at which UPower will
# consider the battery low, critical, or take action for the critical
# battery level.
#
# This will also be used for batteries which don't have time information
# such as that of peripherals.
#
# If any value is invalid, or not in descending order, the defaults
# will be used.
#
# Defaults:
# PercentageLow=10
# PercentageCritical=3
# PercentageAction=2
PercentageLow=10
PercentageCritical=3
PercentageAction=2
# When UsePercentageForPolicy is false, the time remaining at which UPower
# will consider the battery low, critical, or take action for the critical
# battery level.
#
# If any value is invalid, or not in descending order, the defaults
# will be used.
#
# Defaults:
# TimeLow=1200
# TimeCritical=300
# TimeAction=120
TimeLow=1200
TimeCritical=300
TimeAction=120
# The action to take when "TimeAction" or "PercentageAction" above has been
# reached for the batteries (UPS or laptop batteries) supplying the computer
#
# Possible values are:
# PowerOff
# Hibernate
# HybridSleep
#
# If HybridSleep isn't available, Hibernate will be used
# If Hibernate isn't available, PowerOff will be used
CriticalPowerAction=HybridSleep
Step 1: Check gsettings
There are two options you can check in the terminal:
$ gsettings get org.gnome.settings-daemon.plugins.power critical-battery-action
'suspend'
$ gsettings get org.gnome.settings-daemon.plugins.power percentage-low
'10'
Then to change them use:
$ gsettings set org.gnome.settings-daemon.plugins.power critical-battery-action suspend
$ gsettings set org.gnome.settings-daemon.plugins.power percentage-low 10
Note on my system they are just fine, so I changed them back to what they were in the first place for sake of example.
Step 2: Check upower
Gnome power management utilizes upower
to do the heavy lifting. To ensure it is running and collecting battery statistics as it should, use:
$ upower -d
Device: /org/freedesktop/UPower/devices/line_power_ACAD
native-path: ACAD
power supply: yes
updated: Sun 18 Mar 2018 02:28:25 PM MDT (135878 seconds ago)
has history: no
has statistics: no
line-power
warning-level: none
online: yes
icon-name: 'ac-adapter-symbolic'
Device: /org/freedesktop/UPower/devices/battery_BAT1
native-path: BAT1
vendor: COMPAL
model: PABAS0241231
serial: 41167
power supply: yes
updated: Tue 20 Mar 2018 04:12:10 AM MDT (53 seconds ago)
has history: yes
has statistics: yes
battery
present: yes
rechargeable: yes
state: charging
warning-level: none
energy: 48.2544 Wh
energy-empty: 0 Wh
energy-full: 93.3552 Wh
energy-full-design: 96.48 Wh
energy-rate: 0.0044914 W
voltage: 14.76 V
percentage: 51%
capacity: 96.7612%
technology: lithium-ion
icon-name: 'battery-good-charging-symbolic'
Device: /org/freedesktop/UPower/devices/mouse_0003o046Do101Ax0008
native-path: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003/0003:046D:101A.0008
vendor: Logitech, Inc.
model: Performance MX
serial: E6CE4571
power supply: no
updated: Tue 20 Mar 2018 04:12:09 AM MDT (54 seconds ago)
has history: yes
has statistics: no
mouse
present: yes
rechargeable: yes
state: discharging
warning-level: none
percentage: 55%
icon-name: 'battery-good-symbolic'
Device: /org/freedesktop/UPower/devices/keyboard_0003o046Do2010x0009
native-path: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/0003:046D:C52B.0003/0003:046D:2010.0009
vendor: Logitech, Inc.
model: K800
serial: 6DB54BFE
power supply: no
updated: Tue 20 Mar 2018 04:12:09 AM MDT (54 seconds ago)
has history: yes
has statistics: no
keyboard
present: yes
rechargeable: yes
state: discharging
warning-level: none
percentage: 90%
icon-name: 'battery-full-symbolic'
Device: /org/freedesktop/UPower/devices/ups_hiddev3
native-path: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4:1.0/usbmisc/hiddev3
vendor: CPS
model: CP550HGa
serial: BFBB104#BI1.g
power supply: yes
updated: Tue 20 Mar 2018 04:12:51 AM MDT (12 seconds ago)
has history: yes
has statistics: yes
ups
present: yes
state: fully-charged
warning-level: none
time to empty: 37.5 minutes
percentage: 100%
icon-name: 'battery-full-charged-symbolic'
Device: /org/freedesktop/UPower/devices/DisplayDevice
power supply: yes
updated: Tue 20 Mar 2018 04:10:14 AM MDT (169 seconds ago)
has history: no
has statistics: no
ups
present: yes
state: fully-charged
warning-level: none
time to empty: 37.5 minutes
percentage: 100%
icon-name: 'battery-full-charged-symbolic'
Daemon:
daemon-version: 0.99.4
on-battery: no
lid-is-closed: no
lid-is-present: yes
critical-action: HybridSleep
First run this command when the laptop is plugged in. Then unplug the laptop and run the command after some time and ensure battery remaining time has reduced appropriately.
Last Step - Low level hibernation
This is the last step which would be taken after exhausting all other steps. This step doesn't require Ubuntu or Unity or Gnome or gsettings
or upower
. It only requires systemd
, udev
and cron
which are built into almost all Linux Distros with or without GUI (Desktop).
From ArchLinux:
Hibernate on low battery level
If your battery sends events to udev
whenever it (dis)charges by 1%, you can use this udev
rule to automatically hibernate the system when battery level is critical, and thus prevent all unsaved work from being lost.
Note: Not all batteries report discharge events. Test by running udevadm monitor --property
while on battery and see if any events are reported. You should wait at least 1% drop. If no events are reported and /sys/class/power_supply/BAT0/alarm
is non-zero then the battery will likely trigger an event when BAT0/energy_now
drops below the alarm value, and the udev
rule will work as long as the percentage math works out. Some laptops have an option for this disabled in BIOS by default.
$ cat /etc/udev/rules.d/99-lowbat.rules
# Suspend the system when battery level drops to 5% or lower
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-5]", RUN+="/usr/bin/systemctl hibernate"
This rule will be repeated whenever the condition is set. As such, when resuming from hibernate when the battery is critical, the computer will hibernate directly. Some laptops do not boot beyond a certain battery level, so the rule could be adjusted accordingly.
Batteries can jump to a lower value instead of discharging continuously, therefore a udev
string matching pattern for all capacities 0
through 5
is used.
Other rules can be added to perform different actions depending on power supply status and/or capacity.
If your system has no or missing ACPI events, use cron
with the following script:
#!/bin/sh
acpi -b | awk -F'[,:%]' '{print $2, $3}' | {
read -r status capacity
if [ "$status" = Discharging -a "$capacity" -lt 5 ]; then
logger "Critical battery threshold"
systemctl hibernate
fi
}
Testing events
One way to test udev
rules is to have them create a file when they are run. For example:
$ cat /etc/udev/rules.d/98-discharging.rules
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/usr/bin/touch /home/example/discharging"
This creates a file at /home/example/discharging
when the laptop charger is unplugged. You can test whether the rule worked by unplugging your laptop and looking for this file. For more advanced udev
rule testing, see Udev#Testing rules before loading.
Summary
There are more steps to post between Step #2 and "Last Step" but these will be posted as OP reveals more details after testing. In the mean time the "Last Step" should reduce more half-way answers like simply reporting percentage remaining.
Heres a friendly script I put together!
I've created a makeshift solution that checks if your laptop is on AC or Battery (DC) power...
Therefore, if it is on AC power it will exit and if not it will continue checking the remaining battery power every 15 seconds until it hits the defined variable low
or rather percent...
Oh and if you want to continue working in the same terminal as you ran this script as simply run the script like so:
bash power-notify.sh &
Without further ado, here is the script:
#!/bin/bash
#Get battery percent
bat_percent=$(acpi | awk -F ", " '{print $2}' | tr -d %)
#When to start showing warnings... Example: 10% --> low=10
low=10
#Alert message to show when low percent is reached
alert="Low battery, $bat_percent% left..."
#Check if notify-send is installed
notfiy_send_test=$(which notify-send)
if [ $? != 0 ]
then
echo "Please install notify-send..."
exit 1
fi
acpi_test=$(which acpi)
if [ $? != 0 ]
then
echo "Please install acpi..."
exit 1
fi
#Test if cord is plugged in
plugged=$(acpi -a | awk -F ": " '{print $2}')
if [ $plugged == 'on-line' ]
then
echo "Your computer is connected to a power source..."
echo "Exiting..."
exit 0
fi
while :
do
if [ $bat_percent -lt $low ]
then
notify-send "$alert"
sleep 15
else
#Check every fifteen seconds to use less resources...
sleep 15
fi
done
This script can also be found on GitHub, here...