How to go automatically from Suspend into Hibernate?

Is it possible to make Ubuntu go into Hibernate state from Suspend, aka "Suspend Sedation"?

What I am looking for is this:
When I close the lid, the laptop is put into Suspend. Then, after a pre-determined time (even if the battery is going strong) if I still don't use it, it should put itself into a Hibernate to save battery power.

For example, my laptop is set up to go into a Suspend once I close the lid. If then I don't use it for entire day, the battery goes flat, because even in suspend mode the hardware still consumes a small amount of power, and the battery eventually discharges. What I want is to be able to tell Ubuntu that even if it is suspended, it still needs to go into Hibernate after some hours of inactivity.

Windows can do that. Ubuntu can be programmed to go into Standby or Hibernate on timer, but not both.


In Ubuntu 18.04 and newer it much more easier. In systemd is available a new mode suspend-then-hibernate. To start using this function you need to create a file /etc/systemd/sleep.conf with the next content:

[Sleep]
HibernateDelaySec=3600

Then you can test it by command:

sudo systemctl suspend-then-hibernate

you can edit HibernateDelaySec to reduce delay to hibernate.


If all works fine you can change Lid Close Action, to do it you need to edit the file /etc/systemd/logind.conf

You need to find option HandleLidSwitch=, uncomment it and change to HandleLidSwitch=suspend-then-hibernate. Then you need to restart systemd-logind service (warning! you user session will be restarted) by the next command:

sudo systemctl restart systemd-logind.service

That's all! Now you can use this nice function.


The solution to this is simple. First, upon suspend and resume, the pm-suspend program executes a series of scripts in /etc/pm/sleep.d and /usr/lib/pm-utils/sleep.d. So my solution is to add a script that does the following:

  1. Upon suspend, record the current time and register a wakeup event using rtcwake.
  2. Upon resume,check the current time against the recorded time from above. If enough time has elapsed, then we probably woke up due to the rtc timer event. Otherwise we woke up early due to a user event (such as opening the laptop screen).
  3. If we woke up due to the rtc timer, then immediately issue a "pm-hibernate" command to go into hibernation.

Here is a script that does this. Name it 0000rtchibernate and place it in the /etc/pm/sleep.d directory (the 0000 is important, so that the script executes first on suspend, and last on resume).

#!/bin/bash
# Script name: /etc/pm/sleep.d/0000rtchibernate
# Purpose: Auto hibernates after a period of sleep
# Edit the "autohibernate" variable below to set the number of seconds to sleep.
curtime=$(date +%s)
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

Hopefully this code comes through on this message board (this is my first post here).

Edit the timeout value autohibernate=7200 at the top, to however many seconds you which to sleep before going into hibernation. The current value above is 2 hours. Note, that you laptop WILL wake up at that time for a few seconds, while it is executing the hibernate function.

So if you plan on putting your laptop in a case, don't suspend, but hibernate instead. Otherwise your laptop could overheat in esp. if it is in a tight fitting slip case (although it will only be on for a few seconds to a minute).

I've been using this method for the past couple of days, so far it has been successful (and saved me from a dead battery this afternoon). Enjoy.

For other Linux distributions that use systemd and newer Ubuntu versions this should still work if you place the script in /usr/lib/systemd/system-sleep instead of /etc/pm/sleep.d. Also, replace the /usr/sbin/pm-hibernate command with systemctl hibernate.


To explain how this works (this is similar to Windows) in simple words: the machine doesn't wake up from standby when battery gets low to be able to save the machine state to the swap partition, it saves everything to the swap partition immediately on standby, and when the battery runs out, it will recover from that by loading the state from the swap partition (as it would do in case you hibernated).

AFAIK linux will/should use hybrid standby/hibernate instead of "normal" standby if it knows that it works for your hardware. It's also possible that this is disabled currently because of too many bugs or something... ;)

If you like experimenting, maybe you can see if you can get any good results with pm-suspend-hybrid.

If the following says you're lucky, then in theory hybrid suspend is supported on your system:

pm-is-supported --suspend-hybrid && echo "you're lucky"