笔记本电脑电池高/低警报

笔记本电脑电池高/低警报

我的笔记本电脑电池的使用寿命还不到我预期的一半,而且它是内置电池。也就是说,更换起来非常不方便。我希望我的替换电池能够使用尽可能长的时间,在阅读了相关资料后,我发现最好的方法是定期充电和放电。因此,我在 Ubuntu 软件中心寻找可以提醒我拔下或插入电源的程序或应用程序,但一无所获,而且默认的电池指示器不够用,因为它不关心电池的健康状况。

那么,有没有什么应用可以做到这一点?如果没有,有没有 API 可以让我编写自己的应用?

编辑:简单的谷歌搜索显示我可以使用命令查找电池信息upower -i /org/freedesktop/UPower/devices/battery_BAT0。如果有一个状态指示器可以帮我做这件事就好了(并且可能记录充电/放电周期)。

答案1

你可以尝试使用这个脚本。

它提出了一个通知播放声音(使用 pulseaudio)当电池电量达到选定值时。它不跟踪充电/放电周期,但通过一些编辑您可以添加此功能。

#! /bin/bash

# read battery percentage value
OUT=`upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage`

# select only the int value
IFS=':' read -ra P <<< "$OUT"
PERCENTAGE="%"
BATTERY_VALUE=${P[1]%$PERCENTAGE}

# send a notification and play sound if battery level is under 10%
if (( $BATTERY_VALUE  < "10")); then
  notify-send "Battery Low level! You need to plug your PC!"

# command to play sound - you can select your preferred sound if this doesn't work
  paplay /usr/share/sounds/freedesktop/stereo/complete.oga
fi

# send a notification and play sound if battery level is equal to 100%
if (( $BATTERY_VALUE  >= "100")); then
  notify-send "Battery charged! You can now unplug your PC!"
  paplay /usr/share/sounds/freedesktop/stereo/complete.oga
fi

将此脚本复制到 bash 文件中并将其移动到本地 bin 文件夹:

sudo mv <script_file> /usr/local/bin/

在哪里脚本文件是脚本的名称(或路径)。然后您可以使用cron守护进程每 5 分钟运行一次以检查电池电量。因此编辑cron

crontab -e

选择首选编辑器并在文件末尾添加以下行:

*/5 * * * * /usr/local/bin/<script_file>

改变脚本文件使用您的脚本名称。

现在它应该可以工作了。系统重启后检查一下。

我希望有用。

答案2

你可以使用这个脚本,因为我今天也遇到了同样的问题。

您可以将此脚本视为 @Danibix 给出的脚本的升级版。下面的脚本使用 systemd 服务,即使进程被终止,也可以自行重新启动。

#!/bin/bash

# Run this script as a cronjob every 5 minutes or so, to get notifications when
# battery percentage goes below 30% or above 80%. Also you can create a systemd service.
# Cronjob line example:
# */5 * * * * /bin/bash /path/to/battery_health_notifications.sh

export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session | head -n 1)/environ | tr '\0' '\n')
export XDG_RUNTIME_DIR="/run/user/1000"

while ((1)) ; do
    BATTERY_PATH=$(upower -e | grep battery)
    LINE_POWER_PATH=$(upower -e | grep line_power)
    BATTERY_PERCENTAGE=$(upower -i $BATTERY_PATH | grep 'percentage:' | awk '{ print $2 }' | sed 's/%//')
    CABLE_PLUGGED=$(upower -i $LINE_POWER_PATH | grep -A2 'line-power' | grep online | awk '{ print $2 }')

    if [[ $CABLE_PLUGGED == 'yes' ]]; then

        if [[ $BATTERY_PERCENTAGE -gt 75 ]]; then
        notify-send --hint=int:transient:1 --urgency=critical "Battery optimization" "Battery reached 75%, unplug the power cable to optimize battery life."

        #Using my own alarm file. You can choose your desired alarm file.
        /bin/paplay /home/ali/.local/alarm.wav
        fi

    else

        if [[ $BATTERY_PERCENTAGE -lt 30 ]]; then
        notify-send --hint=int:transient:1 --urgency=critical "Battery optimization" "Battery is below 30%, plug in the power cable to optimize battery life."

        #Using my own alarm file. You can choose your desired alarm file.
        /bin/paplay /home/ali/.local/alarm.wav
        fi

    fi
    sleep 2
done

将上述脚本放在你的主文件夹中的任何位置电池健康通知.sh

之后创建服务文件电池警报服务并将下面的文本复制到其中。请务必阅读下面服务文件中的注释,以便根据您的系统进行配置。

[Unit]
#just what it does
Description= Battery alarm notifies the user about charge and discharge cycle of battery

[Service]
#not run by root, but by me. Change User to your username
User=ali
#we assume the full service as active one the script was started
Type=simple
#where to find the executable 
ExecStart=/home/ali/.local/battery_health_notifications.sh
#what you want: make sure it always is running
Restart=always

[Install]
#which service wants this to run - default.target is just it is loaded by default
WantedBy=default.target

打开终端电池警报服务并将以下命令复制到您的终端。

sudo mv battery_alarm.service /etc/systemd/system/
systemctl enable battery_alarm.service
systemctl start battery_alarm.service

您的电池警报设置已完成。要检查其是否正在运行,请在您的终端上输入以下命令。

systemctl status battery_alarm.service 

你会得到类似这样的结果。

● battery_alarm.service - Battery alarm notifies user about charge discharge cycle of battery
     Loaded: loaded (/etc/systemd/system/battery_alarm.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-05-16 20:26:34 PKT; 23min ago
   Main PID: 166450 (battery_health_)
      Tasks: 2 (limit: 12979)
     Memory: 2.6M
     CGroup: /system.slice/battery_alarm.service
             ├─166450 /bin/bash /home/ali/.local/battery_health_notifications.sh
             └─183382 sleep 2

May 16 20:26:34 pop-os systemd[1]: Started Battery alarm notifies the user about charge-discharge cycle of the battery.

相关内容