2021-01-19 09:04:21 +08:00
|
|
|
|
[#]: collector: (lujun9972)
|
|
|
|
|
[#]: translator: (geekpi)
|
2021-01-20 23:14:15 +08:00
|
|
|
|
[#]: reviewer: (wxy)
|
2021-01-20 23:14:52 +08:00
|
|
|
|
[#]: publisher: (wxy)
|
|
|
|
|
[#]: url: (https://linux.cn/article-13036-1.html)
|
2021-01-19 09:04:21 +08:00
|
|
|
|
[#]: subject: (How to get Battery status notification when a battery is full or low)
|
|
|
|
|
[#]: via: (https://www.2daygeek.com/linux-low-full-charge-discharge-battery-notification/)
|
|
|
|
|
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
|
|
|
|
|
|
|
|
|
|
如何在电池充满或低电量时获得电池状态通知
|
|
|
|
|
======
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
![](https://img.linux.net.cn/data/attachment/album/202101/20/231310avo3kzv67vkm3tz7.jpg)
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
对于类 Unix 用户来说,Linux 笔记本是不错的选择,但它经常会耗尽电池。我试过很多 Linux 操作系统,但没有像 Windows 那样电池寿命长。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
充电时间长了会对电池造成损害,所以在电池 100% 充满时要拔掉电源线。电池充电或放电时没有默认的应用程序来通知,需要安装第三方应用来通知你。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
为此,我通常会安装 [Battery Monitor][1],但它已经被废弃,所以我创建了一个 shell 脚本来获取通知。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
笔记本电池充放电状态可以通过以下两个命令来识别。
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
使用 `acpi` 命令。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ acpi -b
|
|
|
|
|
Battery 0: Discharging, 71%, 00:58:39 remaining
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
使用 `upower` 命令。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -w 'state|percentage' | awk '{print $2}'
|
|
|
|
|
discharging
|
|
|
|
|
64%
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 方法 1:当电池电量高于 95% 或低于 20% 时,用 Shell 脚本发送警报
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
这个脚本在启动时在后台运行,每分钟检查一次电池状态,然后在电池电量超过 95% 或放电时电量低于 20% 时发送通知。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
警报会直到你的电池电量超过 20% 或低于 95% 时才会停止。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo vi /opt/scripts/battery-status.sh
|
2021-01-20 23:14:15 +08:00
|
|
|
|
```
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
```
|
2021-01-19 09:04:21 +08:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
while true
|
|
|
|
|
do
|
2021-01-20 23:14:15 +08:00
|
|
|
|
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
|
|
|
|
|
if [ $battery_level -ge 95 ]; then
|
|
|
|
|
notify-send "Battery Full" "Level: ${battery_level}%"
|
|
|
|
|
paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
|
|
|
|
|
elif [ $battery_level -le 20 ]; then
|
|
|
|
|
notify-send --urgency=CRITICAL "Battery Low" "Level: ${battery_level}%"
|
|
|
|
|
paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
|
|
|
|
|
fi
|
|
|
|
|
sleep 60
|
2021-01-19 09:04:21 +08:00
|
|
|
|
done
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
脚本完成后,设置可执行权限:
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo chmod +x /opt/scripts/battery-status.sh
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
最后,将该脚本添加到用户配置文件的底部。对于全局范围来说,你需要在 `/etc/profile` 文件中添加该脚本。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ vi /home/magi/.profile
|
|
|
|
|
|
|
|
|
|
/opt/scripts/battery-status.sh &
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
[重启你的 Linux 系统][2]来检查这点。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo reboot
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
### 方法 2:当电池充电(高于 95%)或放电(低于 20%)时发送通知的 Shell 脚本
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
这个脚本与上面的脚本类似,但它是由交流适配器负责。
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
如果你插上了交流适配器,而且电池的电量超过 95%,它就会发出一个带有声音的通知,但是这个通知不会停止,直到你拔掉交流适配器。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
![][3]
|
|
|
|
|
|
|
|
|
|
如果你拔掉交流适配器,你将永远不会再看到通知,直到你的电池电量下降到 20%。
|
|
|
|
|
|
2021-01-20 23:21:12 +08:00
|
|
|
|
![][5]
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo vi /opt/scripts/battery-status-1.sh
|
2021-01-20 23:14:15 +08:00
|
|
|
|
```
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
```
|
2021-01-19 09:04:21 +08:00
|
|
|
|
#!/bin/bash
|
2021-01-20 23:14:15 +08:00
|
|
|
|
while true
|
|
|
|
|
do
|
|
|
|
|
export DISPLAY=:0.0
|
|
|
|
|
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
|
|
|
|
|
if on_ac_power; then
|
|
|
|
|
if [ $battery_level -ge 95 ]; then
|
|
|
|
|
notify-send "Battery Full" "Level: ${battery_level}% "
|
|
|
|
|
paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
if [ $battery_level -le 20 ]; then
|
|
|
|
|
notify-send --urgency=CRITICAL "Battery Low" "Level: ${battery_level}%"
|
|
|
|
|
paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
sleep 60
|
2021-01-19 09:04:21 +08:00
|
|
|
|
done
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
脚本完成后,设置执行权限:
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo chmod +x /opt/scripts/battery-status-1.sh
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
最后将脚本添加到用户配置文件的底部。对于全局范围来说,你需要在 `/etc/profile` 文件中添加该脚本。
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ vi /home/magi/.profile
|
|
|
|
|
|
|
|
|
|
/opt/scripts/battery-status-1.sh &
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
重启系统来检查:
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo reboot
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-20 23:14:15 +08:00
|
|
|
|
参考: [stackexchange][4]
|
2021-01-19 09:04:21 +08:00
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: https://www.2daygeek.com/linux-low-full-charge-discharge-battery-notification/
|
|
|
|
|
|
|
|
|
|
作者:[Magesh Maruthamuthu][a]
|
|
|
|
|
选题:[lujun9972][b]
|
|
|
|
|
译者:[geekpi](https://github.com/geekpi)
|
|
|
|
|
校对:[校对者ID](https://github.com/校对者ID)
|
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
|
|
|
|
[a]: https://www.2daygeek.com/author/magesh/
|
|
|
|
|
[b]: https://github.com/lujun9972
|
|
|
|
|
[1]: https://www.2daygeek.com/category/battery-monitor/
|
|
|
|
|
[2]: https://www.2daygeek.com/6-commands-to-shutdown-halt-poweroff-reboot-the-linux-system/
|
2021-01-20 23:21:12 +08:00
|
|
|
|
[3]: https://i0.wp.com/www.2daygeek.com/wp-content/uploads/2021/01/linux-low-full-battery-notification-2.png?w=1046&ssl=1
|
|
|
|
|
[4]: https://unix.stackexchange.com/questions/60778/how-can-i-get-an-alert-when-my-battery-is-about-to-die-in-linux-mint
|
|
|
|
|
[5]: https://i1.wp.com/www.2daygeek.com/wp-content/uploads/2021/01/linux-low-full-battery-notification-3.png?w=1029&ssl=1
|