Skip to content

Instantly share code, notes, and snippets.

@damms005
Last active April 9, 2020 08:04
Show Gist options
  • Save damms005/7e055d74d4ec84b984803838342a142d to your computer and use it in GitHub Desktop.
Save damms005/7e055d74d4ec84b984803838342a142d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
##########
## Name: battery-notification.sh
## Author: Adapted from the original script by Nicholas Neal (nwneal@kisoki.com)
## Description:
## This is a small script that will install itself to cron
## and run every minute, and check if your battery level is
## low, and send an alert to your desktop as appropriate
##
## Dependencies: acpi, notify-send
##
## Tested on Ubuntu Desktop 18.04.2 LTS
##########
eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
# check if acpi and send-notify are installed.
if [ `dpkg -l | grep acpi | grep -v acpi-support | grep -v acpid | grep -c acpi` -ne 1 ]; then
echo "run 'sudo apt install acpi' then run '$0' again."
exit
fi
if [ $# -eq 1 ] && [ "$1" == "--install" ]; then
echo "installing battery notifier..."
if [ ! -e "$HOME/bin" ]; then
mkdir $HOME/bin
fi
cp $0 $HOME/bin/bn.sh
(crontab -l 2>/dev/null; echo "*/2 * * * * $HOME/bin/bn.sh") | crontab -
else
# check if power adapter is plugged in, if not, check battery status.
if [ -z "`acpi -a | grep on-line`" ]; then
batlvl=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if [ $batlvl -le 30 ] && [ $batlvl -ge 20 ]; then
/usr/bin/notify-send --hint int:transient:1 "Battery Alert" "Battery is at $batlvl%. Please plug your computer in."
elif [ $batlvl -le 20 ] && [ $batlvl -ge 10 ]; then
/usr/bin/notify-send --hint int:transient:1 "Battery Alert" "Battery is at $batlvl%. Computer will shutdown soon"
elif [ $batlvl -le 10 ]; then
/usr/bin/notify-send --hint int:transient:1 "Battery Alert" "BATTERY CRITICALLY LOW, YOUR COMPUTER WILL SHUTDOWN ANY MOMENT!"
fi
fi
fi
47,1 Bot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment