Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
auto-hibernate script and systemd service
#!/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
# 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
You can’t perform that action at this time.