Auto hibernate after suspend

From Sabayon Wiki
Revision as of 10:18, 18 February 2013 by Dyonisos (Talk | contribs)

Jump to: navigation, search

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'.

File: /etc/pm/sleep.d/0099autohibernate
# 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

echo "$curtime $1" >>/tmp/autohibernate.log
if [ "$1" = "suspend" ]
    # Suspending.  Record current time, and set a wake up timer.
    echo "$curtime" >/var/run/pm-utils/locks/rtchibernate.lock
    rtcwake -m no -s $autohibernate

if [ "$1" = "resume" ]
    # 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 hibernate
        rm /var/run/pm-utils/locks/pm-suspend.lock
        # Otherwise cancel the rtc timer and wake up normally.
        rtcwake -m no -s 1

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).