Last active
February 20, 2026 00:36
-
-
Save tadeubas/f56556a6d58d7a993e3878728289d60c to your computer and use it in GitHub Desktop.
Battery status notification script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Laptop batteries last longer around ~50% charge. This script is intended to be running from time to time, tested on Linux Mint 22.
Content for
battery-notify.serviceContent for
battery-notify.timer