Skip to content

Instantly share code, notes, and snippets.

@pca2
Last active November 3, 2018 16:00
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 pca2/a8368004035bd4c34a4c5a04e227d242 to your computer and use it in GitHub Desktop.
Save pca2/a8368004035bd4c34a4c5a04e227d242 to your computer and use it in GitHub Desktop.
Linux Battery Unplug Reminder script (FruitJuice)
#! /bin/bash
#Adapted from https://linoxide.com/linux-how-to/remind-unplug-charging-laptop-arch-linux/
# DEBUG=true #uncomment to enable logging
MIN_BAT=40
MAX_BAT=80
log(){
datestamp=$(date +%Y-%m-%d_%H:%M:%S)
msg="$@"
echo "[$datestamp]- $msg"
}
debug_log (){
if [ -n "$DEBUG" ]; then
log "$@";
fi
}
debug_log "start"
while true ; do
debug_log "in loop"
UNPLUGGED=$(acpi -a | grep -i off-line)
BAT_PERCENTAGE=$(acpi|grep -Po "[0-9]+(?=%)")
debug_log "unplugged status: $UNPLUGGED"
debug_log "Current bat percentage: $BAT_PERCENTAGE"
if [ $BAT_PERCENTAGE -le $MIN_BAT ]; then # Battery under low limit
debug_log "bat low"
notify-send "Battery under $MIN_BAT%. Please plug in the adapter"
elif [ $BAT_PERCENTAGE -ge $MAX_BAT ]; then # Battery over high limit
debug_log "bat high"
if [ "$UNPLUGGED" == "" ]; then # plugged
debug_log "bat plugged"
notify-send "Battery above $MAX_BAT%. Please unplug the adapter"
fi
fi
debug_log "sleeping"
sleep 30 #Repeat every 30 seconds
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment