Skip to content

Instantly share code, notes, and snippets.

@tadeubas
Last active February 20, 2026 00:36
Show Gist options
  • Select an option

  • Save tadeubas/f56556a6d58d7a993e3878728289d60c to your computer and use it in GitHub Desktop.

Select an option

Save tadeubas/f56556a6d58d7a993e3878728289d60c to your computer and use it in GitHub Desktop.
Battery status notification script
#!/bin/bash
# Read previous notification ID if exists
NOTIFY_ID_FILE="/tmp/battery_notify_id"
NEW_ID=-1
if [ -f "$NOTIFY_ID_FILE" ]; then
ID=$(cat "$NOTIFY_ID_FILE")
else
ID=0
fi
BAT=$(ls /sys/class/power_supply | grep BAT | head -n1)
BASE="/sys/class/power_supply/$BAT"
state=$(<"$BASE/status")
percent=$(<"$BASE/capacity")
health=$(($(<"$BASE/energy_full") * 100 / $(<"$BASE/energy_full_design")))
cycles=$(<"$BASE/cycle_count")
if [[ "$state" == "Charging" && "$percent" -ge 80 ]]; then
NEW_ID=$(notify-send -u critical -r $ID -p "🔋Battery almost full ($percent%)" \
"You may unplug the charger ($health% \/ $cycles cycles)")
elif [[ "$state" == "Discharging" && "$percent" -le 40 ]]; then
NEW_ID=$(notify-send -u critical -r $ID -p "🔋Battery low ($percent%)" \
"Consider plugging in the charger ($health% \/ $cycles cycles)")
fi
if [ "$NEW_ID" -ge 0 ]; then
# Save ID for next replacement
echo "$NEW_ID" > "$NOTIFY_ID_FILE"
fi
@tadeubas
Copy link
Author

Laptop batteries last longer around ~50% charge. This script is intended to be running from time to time, tested on Linux Mint 22.

nano ~/battery_notify.sh
chmod 700 ~/battery_notify.sh
mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/battery-notify.service
nano ~/.config/systemd/user/battery-notify.timer
systemctl --user enable --now battery-notify.timer

Content for battery-notify.service

[Service]
Type=oneshot
ExecStart=/home/<PUT_YOUR_USER_HERE>/battery_notify.sh

Content for battery-notify.timer

[Timer]
OnBootSec=2min
OnUnitActiveSec=5min

[Install]
WantedBy=default.target

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