mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
commit
4e91625fc6
@ -1,157 +0,0 @@
|
|||||||
[#]: collector: (lujun9972)
|
|
||||||
[#]: translator: (geekpi)
|
|
||||||
[#]: reviewer: ( )
|
|
||||||
[#]: publisher: ( )
|
|
||||||
[#]: url: ( )
|
|
||||||
[#]: 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/)
|
|
||||||
|
|
||||||
How to get Battery status notification when a battery is full or low
|
|
||||||
======
|
|
||||||
|
|
||||||
Linux laptops are good for Nix users, but it often drains the battery.
|
|
||||||
|
|
||||||
I tried many Linux operating systems, but did not have a long battery life like Windows.
|
|
||||||
|
|
||||||
Charging a battery for a longer duration will damage your battery, so unplug the power cable when it is 100% charged.
|
|
||||||
|
|
||||||
There is no default application to notify when the battery charged or discharged, and you need to install a third-party application to notify you.
|
|
||||||
|
|
||||||
For this, I usually install the **[Battery Monitor][1]** app, but it was deprecated, so I created a shell script to get the notification.
|
|
||||||
|
|
||||||
Laptop battery charging and discharging status can be identified using the following two commands.
|
|
||||||
|
|
||||||
Using acpi command.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ acpi -b
|
|
||||||
Battery 0: Discharging, 71%, 00:58:39 remaining
|
|
||||||
```
|
|
||||||
|
|
||||||
Using upower command.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -w 'state|percentage' | awk '{print $2}'
|
|
||||||
discharging
|
|
||||||
64%
|
|
||||||
```
|
|
||||||
|
|
||||||
### Method-1: Shell script to send an alert when the battery level is above 95% or below 20%
|
|
||||||
|
|
||||||
This script runs in the background on startup and checks the battery status every minute and then sends a notification when the battery level is charged above 95% or discharged less than 20%.
|
|
||||||
|
|
||||||
The alert will not go off until your battery is over 20% or less than 95% charged.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ sudo vi /opt/scripts/battery-status.sh
|
|
||||||
|
|
||||||
#!/bin/bash
|
|
||||||
while true
|
|
||||||
do
|
|
||||||
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
|
|
||||||
done
|
|
||||||
```
|
|
||||||
|
|
||||||
Once the script is ready, set the executable permission.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ sudo chmod +x /opt/scripts/battery-status.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
Finally, add the script to the bottom of the user profile file. For system-wide, you need to add the script on the /etc/profile file.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ vi /home/magi/.profile
|
|
||||||
|
|
||||||
/opt/scripts/battery-status.sh &
|
|
||||||
```
|
|
||||||
|
|
||||||
**[Reboot your Linux system][2]** to check this.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ sudo reboot
|
|
||||||
```
|
|
||||||
|
|
||||||
### Method-2: Shell script to send a notification when the battery level is charged (above 95%) or discharged (below 20%)
|
|
||||||
|
|
||||||
This script is similar to the above script, but it is responsible with the AC adapter.
|
|
||||||
|
|
||||||
If your AC adapter is plugged in and the battery is charged above 95%, it will send a notification with a sound, but the notification will not stop until you unplug the AC adapter.
|
|
||||||
|
|
||||||
![][3]
|
|
||||||
|
|
||||||
If you unplug the AC adapter, you will never see the notification again until your battery charge drops to 20%.
|
|
||||||
|
|
||||||
![][3]
|
|
||||||
|
|
||||||
```
|
|
||||||
$ sudo vi /opt/scripts/battery-status-1.sh
|
|
||||||
|
|
||||||
#!/bin/bash
|
|
||||||
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
|
|
||||||
done
|
|
||||||
```
|
|
||||||
|
|
||||||
Once the script is ready, set the permission to execute.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ sudo chmod +x /opt/scripts/battery-status-1.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
Finally add the script to the bottom of the user **profile** file. For system-wide, you need to add the script on /etc/profile file.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ vi /home/magi/.profile
|
|
||||||
|
|
||||||
/opt/scripts/battery-status-1.sh &
|
|
||||||
```
|
|
||||||
|
|
||||||
Restart your system to check this.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ sudo reboot
|
|
||||||
```
|
|
||||||
|
|
||||||
**Ref:** [stackexchange][4]
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://www.2daygeek.com/linux-low-full-charge-discharge-battery-notification/
|
|
||||||
|
|
||||||
作者:[Magesh Maruthamuthu][a]
|
|
||||||
选题:[lujun9972][b]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者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/
|
|
||||||
[3]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
|
|
||||||
[4]: https://unix.stackexchange.com/questions/60778/how-can-i-get-an-alert-when-my-battery-is-about-to-die-in-linux-mint
|
|
@ -0,0 +1,157 @@
|
|||||||
|
[#]: collector: (lujun9972)
|
||||||
|
[#]: translator: (geekpi)
|
||||||
|
[#]: reviewer: ( )
|
||||||
|
[#]: publisher: ( )
|
||||||
|
[#]: url: ( )
|
||||||
|
[#]: 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/)
|
||||||
|
|
||||||
|
如何在电池充满或低电量时获得电池状态通知
|
||||||
|
======
|
||||||
|
|
||||||
|
对于类 Unix 用户来说,Linux 笔记本是不错的选择,但它经常会耗尽电池。
|
||||||
|
|
||||||
|
我试过很多 Linux 操作系统,但没有像 Windows 那样电池寿命长。
|
||||||
|
|
||||||
|
充电时间长了会对电池造成损害,所以在电池 100% 充满时要拔掉电源线。
|
||||||
|
|
||||||
|
电池充电或放电时没有默认的应用程序来通知,需要安装第三方应用来通知你。
|
||||||
|
|
||||||
|
为此,我通常会安装 **[Battery Monitor][1]**,但它已经被废弃,所以我创建了一个 shell 脚本来获取通知。
|
||||||
|
|
||||||
|
笔记本电池充放电状态可以通过以下两个命令来识别。
|
||||||
|
|
||||||
|
使用 acpi 命令。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ acpi -b
|
||||||
|
Battery 0: Discharging, 71%, 00:58:39 remaining
|
||||||
|
```
|
||||||
|
|
||||||
|
使用 upower 命令。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -w 'state|percentage' | awk '{print $2}'
|
||||||
|
discharging
|
||||||
|
64%
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方法 1:当电池电量高于 95% 或低于 20% 时,用 Shell 脚本发送警报
|
||||||
|
|
||||||
|
这个脚本在启动时在后台运行,每分钟检查一次电池状态,然后在电池电量超过 95% 或放电低于 20% 时发送通知。
|
||||||
|
|
||||||
|
警报会直到你的电池电量超过 20% 或低于 95% 时才会停止。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sudo vi /opt/scripts/battery-status.sh
|
||||||
|
|
||||||
|
#!/bin/bash
|
||||||
|
while true
|
||||||
|
do
|
||||||
|
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
|
||||||
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
脚本完成后,设置可执行权限。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sudo chmod +x /opt/scripts/battery-status.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
最后,将该脚本添加到用户配置文件的底部。对于全局范围来说,你需要在 /etc/profile 文件中添加该脚本。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ vi /home/magi/.profile
|
||||||
|
|
||||||
|
/opt/scripts/battery-status.sh &
|
||||||
|
```
|
||||||
|
|
||||||
|
**[重启你的 Linux 系统][2]**来检查这点。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sudo reboot
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方法 2:当电池充电(高于 95%)或放电(低于 20%)时发送通知的 Shell 脚本。
|
||||||
|
|
||||||
|
这个脚本与上面的脚本类似,但它是由交流适配器负责。
|
||||||
|
|
||||||
|
如果你的交流适配器插上了,而且电池的电量超过 95%,它就会发出一个带有声音的通知,但是这个通知不会停止,直到你拔掉交流适配器。
|
||||||
|
|
||||||
|
![][3]
|
||||||
|
|
||||||
|
如果你拔掉交流适配器,你将永远不会再看到通知,直到你的电池电量下降到 20%。
|
||||||
|
|
||||||
|
![][3]
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sudo vi /opt/scripts/battery-status-1.sh
|
||||||
|
|
||||||
|
#!/bin/bash
|
||||||
|
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
|
||||||
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
脚本完成后,设置执行权限。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sudo chmod +x /opt/scripts/battery-status-1.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
最后将脚本添加到用户 **profile** 文件的底部。对于全局范围来说,你需要在 /etc/profile 文件中添加该脚本。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ vi /home/magi/.profile
|
||||||
|
|
||||||
|
/opt/scripts/battery-status-1.sh &
|
||||||
|
```
|
||||||
|
|
||||||
|
重启系统来检查。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sudo reboot
|
||||||
|
```
|
||||||
|
|
||||||
|
**参考:** [stackexchange][4]
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
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/
|
||||||
|
[3]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
|
||||||
|
[4]: https://unix.stackexchange.com/questions/60778/how-can-i-get-an-alert-when-my-battery-is-about-to-die-in-linux-mint
|
Loading…
Reference in New Issue
Block a user