Skip to content

Instantly share code, notes, and snippets.

@flxai
Last active September 13, 2016 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flxai/8f03077dd74d836b4474733721512c50 to your computer and use it in GitHub Desktop.
Save flxai/8f03077dd74d836b4474733721512c50 to your computer and use it in GitHub Desktop.
flipdot Shutdown on disconnected power supply
#!/bin/bash
# When the power supply is disconnected, shut down machine after a defined time
# and warning messages
# Time in seconds to shutdown device after disconnected power supply
grace_time=500
timeout=0
while :; do
state=$(upower -i "$battery" | grep state | awk '{print $2}')
if [[ $(cat /sys/class/power_supply/AC/online) -lt 1 ]]; then
time_left=$((grace_time-timer*60))
notify-send -u critical "Power supply disconnected" "System will shutdown in $((time_left/60)) minutes"
if [[ "$time_left" -le 0 ]]; then
notify-send -u critical "SHUTTING DOWN SYSTEM"
systemctl poweroff
exit 0
fi
((timer++))
else
timer=0
fi
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment