Created
February 4, 2018 12:56
-
-
Save domenkozar/59597b829fd76d7a42ede4e66519b250 to your computer and use it in GitHub Desktop.
This file contains 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
# Just put this in your cron (Runs every 1 minute). | |
# * * * * * /home/YOURUSERNAME/bin/suspend-on-low-battery | |
source /nix/var/nix/profiles/system/etc/profile | |
UPOWER=$(nix-build -A upower '<nixpkgs>')/bin/upower | |
NOTIFY_SEND=$(nix-build -A libnotify '<nixpkgs>')/bin/notify-send | |
DISPLAY=:0.0 | |
battery_device=`$UPOWER -e | grep -i battery | head -n 1` | |
battery_percentage=`$UPOWER -i $battery_device | grep percentage | head -n 1 | cut -d ':' -f 2 | tr -d ' ' | tr -d '%'` | |
battery_state=`$UPOWER -i $battery_device | grep state | head -n 1 | cut -d ':' -f 2 | tr -d ' '` | |
[ 10 -le $battery_percentage -a $battery_percentage -lt 11 -a $battery_state == "discharging" ] && $NOTIFY_SEND "Battery level at 10 percent" | |
[ 6 -le $battery_percentage -a $battery_percentage -lt 9 -a $battery_state == "discharging" ] && $NOTIFY_SEND "Battery level at $battery_percentage%" -u critical | |
[ $battery_percentage -le 3 -a $battery_state == "discharging" ] && $NOTIFY_SEND "BATTERY LEVEL AT 3%, SUSPENDING!" -u critical && sleep 5 && \ | |
sudo -n /run/current-system/sw/bin/systemctl suspend |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment