Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Last active April 29, 2020 19:31
Show Gist options
  • Save wookietreiber/0e4018b840b66e4f3eb646e40fd74c8d to your computer and use it in GitHub Desktop.
Save wookietreiber/0e4018b840b66e4f3eb646e40fd74c8d to your computer and use it in GitHub Desktop.
battery notification
[Unit]
Description=battery notification
[Service]
Environment="DISPLAY=:0"
ExecStart=/bin/bash /path/to/battery-notification.sh 20 10
Type=oneshot
[Install]
WantedBy=default.target
#!/bin/bash
app=$(basename "$0" .sh)
[[ $# -eq 2 ]] || {
echo "usage: $app warning critical" >&2
exit 1
}
warning=$1
critical=$2
function notify {
notify-send -c device -i battery "$@"
}
for battery_path in /sys/class/power_supply/BAT*
do
battery=$(basename "$battery_path")
capacity=$(< "$battery_path/capacity")
if [[ $capacity -le $critical ]]
then
notify -u critical 'Battery CRITICAL' "Capacity $capacity%"
elif [[ $capacity -le $warning ]]
then
notify -u normal 'Battery WARNING' "Capacity $capacity%"
else
echo "OK $battery $capacity"
fi
done
[Unit]
Description=run battery-notification
[Timer]
OnBootSec=1min
OnUnitActiveSec=1min
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment