Auto hibernate after suspend

From Sabayon Wiki
Revision as of 21:42, 31 January 2014 by Dyonisos (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 write a small script that does the magic. The scripts vary a bit depending on whether you use OpenRC or systemd.

To check which init you're using:

eselect init list

OpenRC

Question.png
Please note that OpenRC will be dropped in summer 2014. Please refer to HOWTO:_systemd for more details on how to get started with systemd.


The location of the script on OpenRC systems is: '/etc/pm/sleep.d'. Create a new file in this folder and name it: '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 


File: /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 


systemd

The script location on systemd configuration is: '/usr/lib/systemd/system-sleep/'. Create a new file in this location and name it: 'auto-hibernate.sh'.


 nano /usr/lib/systemd/system-sleep/auto-hibernate.sh 


Afterwards add the following content.


File: /usr/lib/systemd/system-sleep/auto-hibernate.sh
#!/bin/bash
#!/bin/bash
# Script name: /lib/systemd/system-sleep/auto-hibernate
# Purpose: Auto hibernates after a period of sleep
# Edit the "autohibernate" variable below to set the number of seconds to sleep.

curtime=$(date +%s)

# 60 Minutes should be fine
autohibernate=3600

echo "$curtime $1" >>/tmp/autohibernate.log


if [ "$1" = "pre" ]
then
        if [ "$2" = "suspend" ]
        then
                # Suspending.  Record current time, and set a wake up timer.
                echo "$curtime" >/var/run/systemd/rtchibernate.lock
                rtcwake -m no -s $autohibernate
        fi
fi

if [ "$1" = "post" ]
then

        if [ "$2" = "suspend" ]
        then
                # Coming out of sleep
                sustime=$(cat /var/run/systemd/rtchibernate.lock)
                rm /var/run/systemd/rtchibernate.lock

                # Did we wake up due to the rtc timer above?
                if [ $(($curtime - $sustime)) -ge $autohibernate ]
                then
                        # Then hibernate
                        /usr/bin/systemctl hibernate
                else
                        # Otherwise cancel the rtc timer and wake up normally.
                        rtcwake -m no -s 1
                fi
        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 /usr/lib/systemd/system-sleep/auto-hibernate.sh 


Conclusion

If you placed the script to the correct location, depending on your system configuration, your computer should go to hibernation after 2 hours of sleep (as we defined 7200 seconds in the script).

Sources

SuperUser: How to hibernate after a period of sleep

Charles Mauch: Hibernate after sleep