Difference between revisions of "Auto hibernate after suspend"

From Sabayon Wiki
Jump to: navigation, search
Line 1: Line 1:
 
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.
 
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'.
+
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.
 +
 
 +
 
 +
== openRC ==
 +
 
 +
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'.
  
  
Line 43: Line 48:
  
  
Save the File by pressing Ctrl+o and exit nano by pressing Ctrl+x! Dont forget to set the executable flag for the script.
+
Save the File by pressing Ctrl+o and exit nano by pressing Ctrl+x! Dont forget to set the executable flag for the script.  
  
  
Line 49: Line 54:
  
  
With this done your computer should go to hibernation after 2 hours of sleep (as we defined 7200 seconds in the script).
+
== 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'.
 +
 
 +
 
 +
{{Console| <pre class="clear"> nano /usr/lib/systemd/system-sleep/auto-hibernate.sh </pre>}}
 +
 
 +
 
 +
Afterwards add the following content.
 +
 
 +
 
 +
{{File| /usr/lib/systemd/system-sleep/auto-hibernate.sh| <pre class="clear">#!/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
 +
</pre>}}
 +
 
 +
 
 +
Save the File by pressing Ctrl+o and exit nano by pressing Ctrl+x! Dont forget to set the executable flag for the script.
 +
 
 +
 
 +
{{Console| <pre class="clear"> chmod +x /usr/lib/systemd/system-sleep/auto-hibernate.sh </pre>}}
 +
 
 +
 
 +
== 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 ==
 
== Sources ==
  
 
[http://superuser.com/questions/298672/linuxhow-to-hibernate-after-a-period-of-sleep SuperUser: How to hibernate after a period of sleep]
 
[http://superuser.com/questions/298672/linuxhow-to-hibernate-after-a-period-of-sleep SuperUser: How to hibernate after a period of sleep]
 +
 +
[http://blog-cmauch.rhcloud.com/hibernate-after-sleep/ Charles Mauch: Hibernate after sleep]
  
  
 
[[Category:HOWTOs| En]]
 
[[Category:HOWTOs| En]]

Revision as of 12:06, 19 January 2014

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.


openRC

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