Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get notification of battery status when the battery is full or low in Linux

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to get battery status notification when the battery is full or low in Linux, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

A Linux laptop is a good choice for Unix-like users, but it often runs out of batteries. I've tried a lot of Linux operating systems, but they don't have as long battery life as Windows.

Long charging time can cause damage to the battery, so unplug the power cord when the battery is 100% full. There is no default application to notify you when charging or discharging the battery, and you need to install a third-party application to notify you.

To do this, I usually install Battery Monitor, but it is obsolete, so I create a shell script to get notifications.

The charge and discharge status of notebook battery can be identified by the following two commands.

Use the acpi command.

$acpi-bBattery 0: Discharging, 71%, 00:58:39 remaining

Use the upower command.

$upower-I / org/freedesktop/UPower/devices/battery_BAT0 | grep-w'state | percentage' | awk'{print $2} 'discharging64% method 1: use the Shell script to send an alarm when the battery charge is higher than 95% or less than 20%

This script runs in the background at startup, checks the battery status every minute, and then sends a notification when the battery exceeds 95% or discharges less than 20%.

The alarm will not stop until your battery is over 20% or less than 95%.

$sudo vi / optUniverse scriptsUniverse batteryMuststatus.shedding accountsBash while truedo 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 60done

After the script completes, set the executable permissions:

$sudo chmod + x / opt/scripts/battery-status.sh

Finally, add the script to the bottom of the user profile. For the global scope, you need to add the script to the / etc/profile file.

$vi / home/magi/.profile / opt/scripts/battery-status.sh &

Restart your Linux system to check this.

$sudo reboot method 2: Shell script that sends notifications when the battery is charged (more than 95%) or discharged (less than 20%)

This script is similar to the script above, but it is the responsibility of the AC adapter.

If you plug in the AC adapter and the battery has more than 95% of the battery, it will send a notification with sound, but the notification will not stop until you unplug the AC adapter.

If you unplug the AC adapter, you will never see the notice again until your battery drops to 20%.

$sudo vi / optge script while true do export DISPLAY=:0.0 battery_level= batteryMustatusMustusMutu1.Shall if on_ac_power; then if binCharacpact while true do export DISPLAY=:0.0 battery_level= `acpi-b |-P-o'[0-9] + (? =%) `[$MatteryMustatusMustus 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 60done

After the script is completed, set the execution permissions:

$sudo chmod + x / opt/scripts/battery-status-1.sh

Finally, add the script to the bottom of the user profile. For the global scope, you need to add the script to the / etc/profile file.

$vi / home/magi/.profile / opt/scripts/battery-status-1.sh &

Restart the system to check:

Sudo reboot thank you for reading this article carefully. I hope the article "how to get battery status notification when the battery is full or low in Linux" shared by the editor will be helpful to you. At the same time, I hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report