Last active
August 29, 2015 14:10
-
-
Save Avinash-Bhat/327ba5db6a1926fadba6 to your computer and use it in GitHub Desktop.
auto-hibernate script and systemd service
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
#!/usr/bin/env bash | |
print_usage() { | |
echo " Usage: $0 [upper-threshold]" | |
} | |
if [ -z $1 ]; | |
then | |
echo "ERROR: upper threshold should be specified" | |
print_usage | |
exit 1 | |
fi | |
battery_level=`acpi -b | cut -d ' ' -f 4 | grep -o '[0-9]*'` | |
battery_state=`acpi -b | cut -d ' ' -f 3 | grep -o '[a-zA-Z]*'` | |
# can it cause i18n issue?? | |
if [ $battery_state != "Discharging" ]; | |
then | |
# no need to continue | |
exit 0 | |
fi | |
echo "current battery level: $battery_level%" | |
if [ $battery_level -le $1 ]; | |
then | |
echo "Battery lower than $1%. Currently at: $(battery_level)%, hibernating" | |
/usr/bin/systemctl hibernate | |
fi |
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
# file /etc/systemd/system/auto-hibernate.service | |
[Unit] | |
Description=Run the auto hibernate script every 5 minutes | |
[Service] | |
Restart=always | |
RestartSec=5m | |
ExecStart=/usr/bin/auto-hibernate 15 | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment