Skip to content

Instantly share code, notes, and snippets.

@reganto
Last active July 19, 2022 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reganto/addd1853ffc0dcf0d3944cd068c37ed7 to your computer and use it in GitHub Desktop.
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
@blaze-fire
Copy link

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

@mdebusk
Copy link

mdebusk commented Jun 30, 2022

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

@reganto
Copy link
Author

reganto commented Jul 1, 2022

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.

@reganto
Copy link
Author

reganto commented Jul 1, 2022

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.

@blaze-fire
Copy link

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.

@mdebusk
Copy link

mdebusk commented Jul 1, 2022

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.

@reganto
Copy link
Author

reganto commented Jul 19, 2022

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.

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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment