Friday, May 3, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 1960  / 1 Year ago, mon, march 6, 2023, 4:55:06

My laptop battery sucks, and lasts up to 5minutes without charging. Sometimes the charger falls out without me knowing, making the computer shut off unexpectedly.



Is there a way to get a notification the moment my charger falls out? not when battery needs to be charged like Ubuntu is used to do(Because that doesn't work for my laptop, and I've given up that idea)


More From » power-management

 Answers
1

Simple linear command:



[[ $(acpi -b | grep -o "Discharging") ]] && notify-send "Alert" "Battery is not charging.
Please plug your AC adapter!"





From man acpi we have:



NAME
acpi - Shows battery status and other ACPI information
SYNOPSIS
acpi [options]
DESCRIPTION
acpi Shows information from the /proc or the /sys filesystem, such as battery status or thermal information.
OPTIONS
-b | --battery
show battery information





If you run acpi -b and you battery is charging mode, you will get this result of that:



Battery 0: Charging, 88%, 00:38:17 until charged


And if your battery isn't charging then the result would be like this:



Battery 0: Discharging, 87%, 03:46:06 remaining


Then We are looking "Discharging" in the result with this command:



acpi -b | grep -o "Discharging"


If battery was not in charging the result will "Discharging".



And finally we send an alert to notification if we got "Discharging" from above command:



[[ $(acpi -b | grep -o "Discharging") ]] && notify-send "Alert" "Battery is not charging.
Please plug your AC adapter!"


Note: [[ Something ]] always true and [[ ! Something ]] always false.



Now it's easiest way that we run it in the background, inside a while loop. Then I put the command into a while loop and I check the battery status between X interval of time. Like this:



while true
do
# Our command
sleep [number of seconds] # check the status every [number of seconds] seconds
done


And if you want to run the script on startup and every 5 minutes, so the construction would be:




  • Save the script(I named it ChkCharge.sh in my home directory)

  • Add a line in /etc/rc.local to call your script (your ChkCharge.sh) + "&" to make it exit. like bash /home/USERNAME/ChkCharge.sh &.






Final Script



#!/bin/bash
while true
do
[[ $(acpi -b | grep -o "Discharging") ]] && notify-send "Alert" "Battery is not charging.
Please plug your AC adapter!"
sleep 300 # 300 seconds or 5 minutes
done


Finish. Reboot and see the magic ;)


[#22943] Tuesday, March 7, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trousransla

Total Points: 285
Total Questions: 112
Total Answers: 113

Location: Spain
Member since Thu, Dec 23, 2021
2 Years ago
;