Skip to content

Instantly share code, notes, and snippets.

@speguero
Last active January 27, 2023 21:02
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 speguero/fa59ae80e0c31f8fe3cd7a997cc87c49 to your computer and use it in GitHub Desktop.
Save speguero/fa59ae80e0c31f8fe3cd7a997cc87c49 to your computer and use it in GitHub Desktop.
autohiberd
#!/bin/sh
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
bat_percent_to_hiber=5
ac_stat()
{
# description: get status of ac power availability.
ac_stat=
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "${os}" in
linux)
ac_stat=$(cat /sys/class/power_supply/AC/online)
;;
*)
return 1
esac
echo "${ac_stat}"
return 0
}
bat_percent()
{
# description: get percentage of battery power availability.
#
# math formula to calculate percentage of battery power
# avaiability when more than one battery is present on system:
# ( bat0 + bat1 + ... ) / 2
bat_cap_i= # power capacity of individual batteries
bat_cap_sum= # sum of power capacity of all individual batteries
bat_cap_per= # percentage of total battery power availability
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "${os}" in
linux)
bat_cap_i="$(cat /sys/class/power_supply/BAT*/capacity)"
;;
*)
return 1
esac
if [ "$(echo "$bat_cap_i" | wc -w)" -gt 1 ]
then
bat_cap_sum="$(echo ${bat_cap_i} | tr ' ' '+' | bc)"
bat_cap_per="$(echo ${bat_cap_sum}/2 | bc)"
else
bat_cap_per="${bat_cap_i}"
fi
echo "${bat_cap_per}"
return 0
}
while :
do
bat_percent > /dev/null 2>&1 || ac_stat > /dev/null 2>&1 || {
printf '%s: unsupported platform\n' "$(basename "$0")" >&2
exit 2
}
if [ "$(bat_percent)" -le ${bat_percent_to_hiber} ] && [ "$(ac_stat)" -eq 0 ]
then
case "${os}" in
linux)
if [ -f /usr/lib/systemd/systemd ]
then
systemctl hibernate
sleep 120
fi
;;
esac
fi
sleep 2
done
<<0
autohiberd
==========
A POSIX script that hibernates a system when the following conditions are met:
- No AC power connected.
- Low availability of battery power.
In the case of (systemd) Linux support, no equivalent to OpenBSD's apmd exists,
hence why this program was originally created.
Licensing (MIT)
---------------
Copyright (c) 2022 Steven Peguero
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment