对于17.10中的电池电量严重不足,电源管理只能选择“不执行任何操作”


10

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

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

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

不执行电源选项

目前,我的笔记本电脑将以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

检查的是您正在寻找的吗askubuntu.com/questions/92794/…–
Aravind

Answers:


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'

然后要更改它们,请使用:

$ 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

侏儒电源管理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 gsettingsupower。它只需要systemdudev并且cron其内置到几乎所有的Linux发行版有或无GUI(桌面)。

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"

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

电池可能会跳到一个较低的值,而不是连续放电,因此,udev对于所有容量05都将使用一个字符串匹配模式。

可以添加其他规则以根据电源状态和/或容量执行不同的操作。

如果您的系统没有或缺少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#Testing加载前的规则

摘要

在步骤2和“最后一步”之间还有更多步骤要发布,但是这些步骤将在OP在测试后显示更多详细信息的情况下发布。同时,“最后一步”应减少更多的中途回答,例如仅报告剩余百分比。


感谢您的回复。不幸的是,似乎我的gsettings参数已经设置为suspend10,这表明我的系统不遵守这些选项。
提问者

@Questioner gsettings是检查的第一步(简单)。我已经添加upower了下一步。
WinEunuuchs2Unix

谢谢你的更新。我会按照您的建议测试笔记本电脑。同时,我已将/etc/UPower/UPower.conf文件内容添加到问题中。
提问者

@Questioner我clipboard-diff在您UPower.conf和系统上安装的版本之间运行。它们是相同的。
WinEunuuchs2Unix

@Questioner夫妻问题:1)gsettings get org.gnome.settings-daemon.plugins.power active返回true吗?2)您是否有一个交换分区> RAM数量?
WinEunuuchs2Unix

3

这是我编写的友好脚本!

我创建了一个临时解决方案,用于检查您的笔记本电脑是使用交流电还是电池(DC)电源...

因此,如果使用交流电源,它将退出,如果不使用交流电源,它将每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上找到,在这里 ...


一个工作系统应该已经按照给定的百分比发出警告,然后以2%的速度进入混合睡眠状态。我认为这就是OP所需要的。可以通过以2%调用休眠状态来改善脚本。此外,每15秒就会出现警告气泡,这是极端的IMO。我认为消息泡泡t 9%,8%,7%... 3%会更实际。此外,系统警报声音还可以帮助当时不看笔记本电脑的人们。这个脚本有一些组件我建议:askubuntu.com/questions/837078/...
WinEunuuchs2Unix

@ WinEunuuchs2Unix只是一个简单的脚本
NerdOfCode

我猜您的脚本和我在上一个评论中链接的脚本都可以被专业人员认为是简单的。我不确定您将哪个脚本称为“简单”脚本?
WinEunuuchs2Unix

我的剧本肯定。
NerdOfCode

不,这是一个很好的脚本,可以执行您想要的操作。所有出色的脚本都是从好的脚本开始的,您可以在几天,几周甚至几十年的时间内对其进行改进。Linux已经存在了很长时间,并且将会存在更长时间。
WinEunuuchs2Unix
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.