当我的笔记本电脑达到某个低电池阈值时,如何让我的笔记本电脑进入睡眠状态?

o_o*_*o-- 29 ubuntu power-management suspend battery

我正在使用 Ubuntu,但我使用 i3 作为我的窗口管理器而不是桌面环境。

当我的电池电量达到 0% 时,计算机会突然关闭,没有任何警告或任何事情。

有没有我可以设置的简单脚本或配置,让它在 4% 的电池电量时进入睡眠状态?

Mar*_*rco 17

这是一个小脚本,用于检查电池电量并在此处调用自定义命令,pm-hibernate以防电池电量低于某个阈值。

#!/bin/sh

###########################################################################
#
# Usage: system-low-battery
#
# Checks if the battery level is low. If “low_threshold” is exceeded
# a system notification is displayed, if “critical_threshold” is exceeded
# a popup window is displayed as well. If “OK” is pressed, the system
# shuts down after “timeout” seconds. If “Cancel” is pressed the script
# does nothing.
#
# This script is supposed to be called from a cron job.
#
###########################################################################

# This is required because the script is invoked by cron. Dbus information
# is stored in a file by the following script when a user logs in. Connect
# it to your autostart mechanism of choice.
#
# #!/bin/sh
# touch $HOME/.dbus/Xdbus
# chmod 600 $HOME/.dbus/Xdbus
# env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
# echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus
# exit 0
#
if [ -r ~/.dbus/Xdbus ]; then
  . ~/.dbus/Xdbus
fi

low_threshold=10
critical_threshold=4
timeout=59
shutdown_cmd='/usr/sbin/pm-hibernate'

level=$(cat /sys/devices/platform/smapi/BAT0/remaining_percent)
state=$(cat /sys/devices/platform/smapi/BAT0/state)

if [ x"$state" != x'discharging' ]; then
  exit 0
fi

do_shutdown() {
  sleep $timeout && kill $zenity_pid 2>/dev/null

  if [ x"$state" != x'discharging' ]; then
    exit 0
  else
    $shutdown_cmd
  fi
}

if [ "$level" -gt $critical_threshold ] && [ "$level" -lt $low_threshold ]; then
  notify-send "Battery level is low: $level%"
fi

if [ "$level" -lt $critical_threshold ]; then

  notify-send -u critical -t 20000 "Battery level is low: $level%" \
    'The system is going to shut down in 1 minute.'

  DISPLAY=:0 zenity --question --ok-label 'OK' --cancel-label 'Cancel' \
    --text "Battery level is low: $level%.\n\n The system is going to shut down in 1 minute." &
  zenity_pid=$!

  do_shutdown &
  shutdown_pid=$!

  trap 'kill $shutdown_pid' 1

  if ! wait $zenity_pid; then
    kill $shutdown_pid 2>/dev/null
  fi

fi

exit 0
Run Code Online (Sandbox Code Playgroud)

这是一个非常简单的脚本,但我认为您明白这个想法并且可以轻松地根据您的需要调整它。您系统上的电池电量路径可能不同。更便携一点可能是使用类似的东西acpi | cut -f2 -d,来获取电池电量。这个脚本可以被 cron 安排为每分钟运行一次。编辑您的 crontabcrontab -e并添加脚本:

*/1 * * * * /home/me/usr/bin/low-battery-shutdown
Run Code Online (Sandbox Code Playgroud)

另一种解决方案是安装桌面环境,如 Gnome 或 Xfce(并将窗口管理器更改为 i3)。两个提到的 destop 环境都具有电源管理守护程序,负责关闭计算机电源。但是我假设您故意不使用它们并且正在寻求更简约的解决方案。

  • @MartinVegter 这取决于您的硬件,您可以尝试`/sys/class/power_supply/BAT0/capacity`。否则使用 `acpi` 命令。 (2认同)

jos*_*sch 10

如果您按照标签建议使用 Ubuntu,而不是破解您自己的脚本,您只需安装 upower 软件包即可。它应该可用于所有 Debian 衍生产品,包括 Ubuntu。默认情况下,它带有一种配置,/etc/UPower/UPower.conf一旦电池电量达到临界值,就会激活混合睡眠。临界水平的默认值为 2%。

对于其他发行版的用户,相关条目/etc/UPower/UPower.conf是:

PercentageAction=2
CriticalPowerAction=HybridSleep
Run Code Online (Sandbox Code Playgroud)

您还可以TimeAction与 with 一起使用UsePercentageForPolicy=false,让操作在只剩下指定的时间时执行:

TimeAction=120
Run Code Online (Sandbox Code Playgroud)

为有效值CriticalPowerActionPowerOffHibernateHybridSleep。如果 HybridSleep 已设置但不可用,则将使用 Hibernate。如果设置了休眠但不可用,则将使用 PowerOff。

HybridSleep 的优点是,除了将内存写入交换区之外,它还可以暂停系统。挂起仍然会消耗一些电池,但是如果您在电池耗尽之前返回,您可以比从休眠系统更快地从挂起系统恢复。如果电池在您回到电源插座之前耗尽,您可以在再次通电后从休眠状态恢复系统。

  • @cipricus 是正确的,但如果机器无法休眠,upower 会优雅地选择关闭机器。 (3认同)

vog*_*vog 9

从 Debian 开始?10(以及相对较新的 Linux 系统),您只需创建一个/etc/cron.d/check-battery包含以下内容的文件:

* * * * * root [ "$(cat /sys/class/power_supply/BAT0/status)" != Discharging -o "$(cat /sys/class/power_supply/BAT0/capacity)" -gt 30 ] || systemctl suspend
Run Code Online (Sandbox Code Playgroud)

这将在电池电量达到 30% 时暂停您的系统。

当然,随意更换最终suspendhybrid-sleephibernatepoweroff或任何适合您的需求。

不需要外部工具,甚至不需要acpi包。这是基于 Matija Nalis 的回答的想法,但调整到 2021 年。

  • 我不知道 upower 的答案是否已经过时。它对我来说不起作用,而且我没有足够的时间来调查和调试这些东西。坦白地说:我为什么要这样做?这个简单的单行词已经很好地完成了它的工作。从我创建它的那一天起,它就是**可靠的**,具有最少的依赖性和系统要求。从那时起我唯一改变的就是将阈值从 10% 提高到 30%。 (2认同)