我在华硕笔记本电脑上使用带有 Gnome 的 Ubuntu 17.10。
在电源设置界面的电源选项卡下,底部有一个选项可以设置电池电量严重不足时要采取的措施。
但是,当我单击下拉菜单来设置该选项时,它只提供一个选项,即“不执行任何操作”:
目前,我的笔记本电脑电池电量耗尽时,或者接近耗尽时,就会自动关闭,没有任何警告,也无法关闭程序并正常关闭。
我希望当电池电量为 10% 时发出警告,并希望在关闭计算机之前留出一些时间手动保存并关闭程序。我该如何实现?
/etc/UPower/UPower.conf
编辑:这是我的文件的内容:
# 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
答案1
步骤 1:检查gsettings
您可以在终端中检查两个选项:
$ gsettings get org.gnome.settings-daemon.plugins.power critical-battery-action
'suspend'
$ gsettings get org.gnome.settings-daemon.plugins.power percentage-low
'10'
然后要更改它们使用:
$ gsettings set org.gnome.settings-daemon.plugins.power critical-battery-action suspend
$ gsettings set org.gnome.settings-daemon.plugins.power percentage-low 10
请注意,在我的系统上它们运行正常,因此为了举例说明,我将它们改回原来的状态。
第 2 步:检查upower
Gnome 电源管理负责upower
完成繁重的工作。为确保它正常运行并收集电池统计信息,请使用:
$ 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
首先在笔记本电脑插入电源时运行此命令。然后拔下笔记本电脑并在一段时间后运行该命令,并确保电池剩余时间已适当减少。
最后一步-低级别休眠
这是完成所有其他步骤后要采取的最后一步。此步骤不需要 Ubuntu 或 Unity 或 Gnome 或gsettings
或upower
。它只需要和systemd
,它们内置于几乎所有带有或不带有 GUI(桌面)的 Linux 发行版中。udev
cron
电池电量低时进入休眠状态
如果您的电池udev
在每次充电(放电)1% 时发送事件,则可以使用此udev
规则在电池电量危急时自动使系统休眠,从而防止所有未保存的工作丢失。
笔记:并非所有电池都会报告放电事件。udevadm monitor --property
使用电池运行时进行测试,看看是否有任何事件报告。您应该等待至少 1% 的电量下降。如果没有报告任何事件并且/sys/class/power_supply/BAT0/alarm
不为零,则电池在BAT0/energy_now
电量低于警报值时可能会触发事件,udev
只要百分比计算正确,规则就会起作用。有些笔记本电脑在 BIOS 中默认禁用此选项。
$ 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"
每当设置条件时,此规则都会重复执行。因此,当电池电量不足时从休眠状态恢复时,计算机将直接休眠。有些笔记本电脑在电池电量超过一定水平时无法启动,因此可以相应地调整规则。
电池可以跳转到较低的值而不是连续放电,因此使用udev
所有容量的字符串匹配模式0
。5
可以添加其他规则,根据电源状态和/或容量执行不同的操作。
如果您的系统没有或缺少 ACPI 事件,请使用cron
以下脚本:
#!/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
}
测试事件
测试udev
规则的一种方法是让它们在运行时创建一个文件。例如:
$ cat /etc/udev/rules.d/98-discharging.rules
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/usr/bin/touch /home/example/discharging"
这会在/home/example/discharging
拔下笔记本电脑充电器时创建一个文件。您可以通过拔下笔记本电脑并查找此文件来测试规则是否有效。有关更高级的udev
规则测试,请参阅Udev#加载前测试规则。
概括
在步骤 #2 和“最后一步”之间还有更多步骤需要发布,但这些步骤将在 OP 测试后透露更多细节时发布。同时,“最后一步”应该会减少更多半途而废的答案,例如仅报告剩余百分比。
答案2
这是我编写的一个友好脚本!
我已经创建了一个临时解决方案,可以检查您的笔记本电脑是使用交流电还是电池(直流电)电源......
因此,如果使用交流电源,它将退出,如果不是,它将继续每 15 秒检查一次剩余电池电量,直到达到定义的变量low
或百分比...
哦,如果您想继续在运行此脚本的同一终端中工作,只需像这样运行脚本:
bash power-notify.sh &
不用多说,以下是脚本:
#!/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
这个脚本也可以在 GitHub 上找到,这里...