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)
Simple linear command:
From
man acpi
we have:If you run
acpi -b
and you battery is charging mode, you will get this result of that:And if your battery isn't charging then the result would be like this:
Then We are looking "Discharging" in the result with this command:
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:
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:
And if you want to run the script on startup and every 5 minutes, so the construction would be:
ChkCharge.sh
in my home directory)/etc/rc.local
to call your script (yourChkCharge.sh
) + "&" to make it exit. likebash /home/USERNAME/ChkCharge.sh &
.Final Script
Finish. Reboot and see the magic ;)
This looks pretty simple, I'm not a bash script expert, but maybe could be useful for you
The
script.sh
content:Basically, I'm reading a file at
sysfs
that has information about your battery, there are more information in there that could be interesting. The filestatus
contains a flag indicating if your device is actually Charging or Discharging. Hope that helps you.