Skip to content

Instantly share code, notes, and snippets.

@atmoz
Last active March 18, 2017 17:55
Show Gist options
  • Save atmoz/0639d1a7bc67375a4b90fb3c5e91f752 to your computer and use it in GitHub Desktop.
Save atmoz/0639d1a7bc67375a4b90fb3c5e91f752 to your computer and use it in GitHub Desktop.
Notify on low battery and hibernate before empty
#!/bin/sh
export DISPLAY=":0"
notice_0=50
warning_0=25
critical_0=10
notice_1=50
warning_1=10
tmpDir="/tmp/battery-guard-nagbar"
mkdir -p "$tmpDir"
function nagbar() {
type="$1"
number="$2"
message="$3"
showOrClear="${4:-0}" # 0: show, else: clear
state="$tmpDir/${type}_${number}"
if [ $showOrClear -eq 0 ]; then
if [ ! -f $state ]; then
touch $state
i3-nagbar -m "$message" -t $type -f "pango:DejaVu Sans Mono 10" 2>&1 1> /dev/null &
fi
else
rm -f $state
fi
}
while true; do acpi -b | awk -F'[,:%]' '{print $2, $3}' | {
read -r status_0 capacity_0
read -r status_1 capacity_1
test "$status_1" = Discharging -a "$capacity_1" -lt $notice_1
nagbar warning 1 "External battery is now under $notice_1 % ($capacity_1 %)" $?
test "$status_1" = Discharging -a "$capacity_1" -lt $warning_1
nagbar warning 1 "External battery getting low ($capacity_1 %)" $?
test "$status_0" = Discharging -a "$capacity_0" -lt $notice_0
nagbar warning 0 "External battery is now under $notice_0 % ($capacity_0 %)" $?
test "$status_0" = Discharging -a "$capacity_0" -lt $warning_0
nagbar warning 0 "Internal battery getting low ($capacity_0 %)" $?
if [ "$status_0" = Discharging -a "$capacity_0" -lt $critical_0 ]; then
logger "Critical battery threshold ($critical_0 %)"
nagbar error 1 "Internal battery critically low ($capacity_0 %)"
systemctl hibernate
else
nagbar error 1 clear 1
fi
let "total = 1 + ($capacity_0 * 2) + ($capacity_1 * 2)"
sleep $total
}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment