电源管理在 17.10 中只有“什么都不做”的选项来应对电池严重不足的情况

Que*_*ner 10 power-management 17.10

我在华硕笔记本电脑上使用 Ubuntu 17.10 和 Gnome。

在“电源设置”界面的“电源”选项卡下,底部有一个选项可以设置电池电量严重不足时的操作。

但是,当我单击下拉菜单来设置该选项时,它只提供一个选项,即“什么都不做”:

什么都不做电源选项

目前,我的笔记本电脑的电池电量将降至 0 或接近 0,然后在没有警告的情况下关闭,也无法关闭程序并正常关闭。

我想在电池电量为 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
Run Code Online (Sandbox Code Playgroud)

Win*_*nix 7

第 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'
Run Code Online (Sandbox Code Playgroud)

然后更改它们使用:

$ gsettings set org.gnome.settings-daemon.plugins.power critical-battery-action suspend
$ gsettings set org.gnome.settings-daemon.plugins.power percentage-low 10
Run Code Online (Sandbox Code Playgroud)

注意在我的系统上它们很好,所以为了举例,我把它们改回原来的样子。


第 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
Run Code Online (Sandbox Code Playgroud)

首先在笔记本电脑插入电源时运行此命令。然后拔下笔记本电脑并在一段时间后运行该命令,确保电池剩余时间适当减少。


最后一步 - 低级休眠

这是在用尽所有其他步骤后将采取的最后一步。此步骤不需要 Ubuntu 或 Unity 或 Gnomegsettingsupower. 它只需要systemd,udev并且cron几乎所有带有或不带有 GUI(桌面)的 Linux 发行版都内置了。

ArchLinux

电池电量低时休眠

如果您的电池在充电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"
Run Code Online (Sandbox Code Playgroud)

只要设置了条件,就会重复此规则。因此,当电池电量不足时从休眠状态恢复时,计算机将直接休眠。有些笔记本电脑在超过一定的电池电量后无法启动,因此可以相应地调整规则。

电池可以跳转到一个较低的值,而不是连续排出的,所以一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
}
Run Code Online (Sandbox Code Playgroud)

测试事件

测试udev规则的一种方法是让它们在运行时创建一个文件。例如:

$ cat /etc/udev/rules.d/98-discharging.rules

SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/usr/bin/touch /home/example/discharging"
Run Code Online (Sandbox Code Playgroud)

这会在/home/example/discharging拔下笔记本电脑充电器时创建一个文件。您可以通过拔掉笔记本电脑并查找此文件来测试规则是否有效。有关更高级的udev规则测试,请参阅加载前的 Udev#Testing 规则

概括

在第 2 步和“最后一步”之间还有更多的步骤要发布,但这些将随着 OP 在测试后揭示更多细节而发布。同时,“最后一步”应该减少更多的中途答案,例如简单地报告剩余百分比。