Auto hibernate after suspend
The most laptops are configured to suspend after a period of inactivity or when the laptop lid is closed. The standard suspend-to-ram method keeps many functions of the computer still alive and keeps consuming battery. To avoid running into low battery status while sleeping it would be a good idea to send the computer to hibernate after a defined period.
To get this done we can use rctwake which is a part of the pm-utils package. All we have to do is to add a script in the '/etc/pm/sleep.d' directory. We name the file '0099autohibernate'. The numbers at the start tell the pm-utils that the script is executed first on suspend '00' and on wake '99'.
nano /etc/pm/sleep.d/0099autohibernate
#!/bin/bash
# Script name: /etc/pm/sleep.d/0099autohibernate
# Toggle hibernation after a defined period of sleep
curtime=$(date +%s)
# Define the time until hibernation in seconds
autohibernate=7200
echo "$curtime $1" >>/tmp/autohibernate.log
if [ "$1" = "suspend" ]
then
# Suspending. Record current time, and set a wake up timer.
echo "$curtime" >/var/run/pm-utils/locks/rtchibernate.lock
rtcwake -m no -s $autohibernate
fi
if [ "$1" = "resume" ]
then
# Coming out of sleep
sustime=$(cat /var/run/pm-utils/locks/rtchibernate.lock)
rm /var/run/pm-utils/locks/rtchibernate.lock
# Did we wake up due to the rtc timer above?
if [ $(($curtime - $sustime)) -ge $autohibernate ]
then
# Then hibernate
rm /var/run/pm-utils/locks/pm-suspend.lock
/usr/sbin/pm-hibernate
else
# Otherwise cancel the rtc timer and wake up normally.
rtcwake -m no -s 1
fi
fi
Save the File by pressing Ctrl+o and exit nano by pressing Ctrl+x! Dont forget to set the executable flag for the script.
chmod +x /etc/pm/sleep.d/0099autohibernate
With this done your computer should go to hibernation after 2 hours of sleep (as we defined 7200 seconds in the script).