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:
- Upon suspend, record the current time and register a wakeup event using rtcwake.
- 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).
- 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.