-
-
Save reganto/addd1853ffc0dcf0d3944cd068c37ed7 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
while true | |
do | |
export DISPLAY=:0.0 | |
battery_level=`cat /sys/class/power_supply/BAT0/capacity` | |
battery_status=`cat /sys/class/power_supply/BAT0/status` | |
if [ $battery_status = "Charging" ] && [ $battery_level -ge 85 ]; | |
then | |
# kdialog --msgbox "Battery fully charged" 5 # for KDE | |
notify-send -u critical "Battery fully charged" # for Gnome | |
fi | |
sleep 120 | |
done |
This seems to work on my system:
#!/bin/bash export DISPLAY=:0.0 while true do battery_level=$(cat /sys/class/power_supply/BAT0/capacity) battery_status=$(cat /sys/class/power_supply/BAT0/status) if [ "$battery_status" = "Unknown" ] || \ [ "$battery_status" = "Charging" ] && \ [ "$battery_level" -ge 85 ]; then notify-send -u critical "Battery fully charged" fi sleep 300 done
It works flawlessly for me too, thanks for your contribution.
Small changes like missing space in line 9 and redundant nested if statements. While copying script beware of the intended spaces as they will affect the output.
#!/bin/bash while true do export DISPLAY=:0.0 battery_level=`cat /sys/class/power_supply/BAT1/capacity` battery_status=`cat /sys/class/power_supply/BAT1/status` if [ $battery_status = "Charging" ] && [ $battery_level -ge 85 ]; then play /path-to-mp3-file; // to play alert sound kdialog --msgbox "Battery fully charged"; fi sleep 120 // cooldown time done
Great! but I think "already fully charged" problem still exist. take a look at @mdebusk's comment.
Small changes like missing space in line 9 and redundant nested if statements. While copying script beware of the intended spaces as they will affect the output.
#!/bin/bash while true do export DISPLAY=:0.0 battery_level=`cat /sys/class/power_supply/BAT1/capacity` battery_status=`cat /sys/class/power_supply/BAT1/status` if [ $battery_status = "Charging" ] && [ $battery_level -ge 85 ]; then play /path-to-mp3-file; // to play alert sound kdialog --msgbox "Battery fully charged"; fi sleep 120 // cooldown time done
Great! but I think "already fully charged" problem still exist. take a look at @mdebusk's comment.
Works fine on my system, even if the battery is greater that 85.
Note: change bat1 in cat /sys/class/power_supply/BAT1/capacity
to bat0 or check your battery name by command ls /sys/class/power_supply/
Also I think checking battery status as Unknown is redundant.
Also I think checking battery status as Unknown is redundant.
A battery that is not charging is "Unknown" rather than "Charged." I don't know why, but I imagine the program can't tell WHY a battery isn't charging and doesn't want to assume.
Small changes like missing space in line 9 and redundant nested if statements. While copying script beware of the intended spaces as they will affect the output.
#!/bin/bash while true do export DISPLAY=:0.0 battery_level=`cat /sys/class/power_supply/BAT1/capacity` battery_status=`cat /sys/class/power_supply/BAT1/status` if [ $battery_status = "Charging" ] && [ $battery_level -ge 85 ]; then play /path-to-mp3-file; // to play alert sound kdialog --msgbox "Battery fully charged"; fi sleep 120 // cooldown time done
Great! but I think "already fully charged" problem still exist. take a look at @mdebusk's comment.
Works fine on my system, even if the battery is greater that 85.
Note: change bat1 in
cat /sys/class/power_supply/BAT1/capacity
to bat0 or check your battery name by commandls /sys/class/power_supply/
Also I think checking battery status as Unknown is redundant.
Small changes like missing space in line 9 and redundant nested if statements. While copying script beware of the intended spaces as they will affect the output.
#!/bin/bash while true do export DISPLAY=:0.0 battery_level=`cat /sys/class/power_supply/BAT1/capacity` battery_status=`cat /sys/class/power_supply/BAT1/status` if [ $battery_status = "Charging" ] && [ $battery_level -ge 85 ]; then play /path-to-mp3-file; // to play alert sound kdialog --msgbox "Battery fully charged"; fi sleep 120 // cooldown time done
Great! but I think "already fully charged" problem still exist. take a look at @mdebusk's comment.
Works fine on my system, even if the battery is greater that 85.
Note: change bat1 in
cat /sys/class/power_supply/BAT1/capacity
to bat0 or check your battery name by commandls /sys/class/power_supply/
Also I think checking battery status as Unknown is redundant.
I tested it on Ubuntu 20.04 without "unknown status check" and it works. thank you for your contribution.
#!/bin/bash
while true
do
export DISPLAY=:0.0
battery_level=`cat /sys/class/power_supply/BAT0/capacity`
battery_status=`cat /sys/class/power_supply/BAT0/status`
if [ $battery_status = "Charging" ] && [ $battery_level -ge 85 ];
then
notify-send -u critical "Battery fully charged"
fi
sleep 120
done
This seems to work on my system: