How to go automatically from Suspend into Hibernate?How do I show grub when resuming from suspend?Howto Enable Hibernate on Ubuntu 18.04Hibernate after suspend stopped working in 17.10Why is my machine shutting down overnight?“systemctl hibernate” returning 1 when run from systemd scriptUbuntu 18.04 Hibernate when lid is closedHow to Hibernate in Ubuntu 18.04Thinkpad X1 Carbon won't suspend after upgrade to 13.10Ubuntu 12.04 comes out of hibernate/standby randomly on Dell XPS 13Like Mac. Close Tap (lid) and active hybrid-suspend 14.04Advanced power options for lid closingLaptop dying during suspend after 2 hours. Suspend/hybrid not workingPrevent Ubuntu from breaking wifi connection on lid closeRestart automatically after suspendForce hibernate using hibernate instead of pm-hibernateDoes linux have a hybrid “deep sleep” mode like the Mac?Ubuntu 18.04 Hibernate when lid is closed
Short story about cities being connected by a conveyor belt
Was this cameo in Captain Marvel computer generated?
Ultrafilters as a double dual
Is "cogitate" used appropriately in "I cogitate that success relies on hard work"?
Precision notation for voltmeters
ESPP--any reason not to go all in?
What can I do if someone tampers with my SSH public key?
After Brexit, will the EU recognize British passports that are valid for more than ten years?
Can I challenge the interviewer to give me a proper technical feedback?
PTIJ: Sport in the Torah
How to make sure I'm assertive enough in contact with subordinates?
What is the oldest European royal house?
Is there a logarithm base for which the logarithm becomes an identity function?
Why do we call complex numbers “numbers” but we don’t consider 2-vectors numbers?
Tabular environment - text vertically positions itself by bottom of tikz picture in adjacent cell
Inorganic chemistry handbook with reaction lists
Can the Witch Sight warlock invocation see through the Mirror Image spell?
Insult for someone who "doesn't know anything"
How can I portion out frozen cookie dough?
How to install "rounded" brake pads
Giving a talk in my old university, how prominently should I tell students my salary?
Where is the License file location for Identity Server in Sitecore 9.1?
How to distinguish easily different soldier of ww2?
Too soon for a plot twist?
How to go automatically from Suspend into Hibernate?
How do I show grub when resuming from suspend?Howto Enable Hibernate on Ubuntu 18.04Hibernate after suspend stopped working in 17.10Why is my machine shutting down overnight?“systemctl hibernate” returning 1 when run from systemd scriptUbuntu 18.04 Hibernate when lid is closedHow to Hibernate in Ubuntu 18.04Thinkpad X1 Carbon won't suspend after upgrade to 13.10Ubuntu 12.04 comes out of hibernate/standby randomly on Dell XPS 13Like Mac. Close Tap (lid) and active hybrid-suspend 14.04Advanced power options for lid closingLaptop dying during suspend after 2 hours. Suspend/hybrid not workingPrevent Ubuntu from breaking wifi connection on lid closeRestart automatically after suspendForce hibernate using hibernate instead of pm-hibernateDoes linux have a hybrid “deep sleep” mode like the Mac?Ubuntu 18.04 Hibernate when lid is closed
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.
suspend power-management hibernate
add a comment |
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.
suspend power-management hibernate
In my research I found the same Linux Mint thread, but "Suspend Sedation" is not an official Microsoft term for that feature and as far as I can tell was invented by the Linux Mint forum user who mentioned it.
– ayan4m1
Nov 10 '10 at 20:43
Is there a better name for that feature?
– Sergey Stadnik
Nov 11 '10 at 2:01
As far as I can tell, there is no universally accepted name for the feature. "Hybrid suspend" is used by some, "suspend sedation" is used by that one Linux Mint forum user, and I've heard "hibernate and suspend" used to refer to the process before. Microsoft officially refers to it as "hybrid sleep," for Windows 7 at least.
– ayan4m1
Nov 11 '10 at 13:16
2
@ayan4m1 I realise this is an old question, but I think it is important to clarify this. Hyrbid sleep is not the same as "Sleep then hibernate after a specified time". Hybrid sleep simply becomes hibernate when power is lost, through battery running out. The behaviour described by the OP does not require Hybrid Sleep to be enabled.
– Paul
Dec 9 '11 at 2:51
add a comment |
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.
suspend power-management 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.
suspend power-management hibernate
suspend power-management hibernate
edited 10 hours ago
Pablo Bianchi
2,89521535
2,89521535
asked Nov 10 '10 at 0:39
Sergey StadnikSergey Stadnik
6451610
6451610
In my research I found the same Linux Mint thread, but "Suspend Sedation" is not an official Microsoft term for that feature and as far as I can tell was invented by the Linux Mint forum user who mentioned it.
– ayan4m1
Nov 10 '10 at 20:43
Is there a better name for that feature?
– Sergey Stadnik
Nov 11 '10 at 2:01
As far as I can tell, there is no universally accepted name for the feature. "Hybrid suspend" is used by some, "suspend sedation" is used by that one Linux Mint forum user, and I've heard "hibernate and suspend" used to refer to the process before. Microsoft officially refers to it as "hybrid sleep," for Windows 7 at least.
– ayan4m1
Nov 11 '10 at 13:16
2
@ayan4m1 I realise this is an old question, but I think it is important to clarify this. Hyrbid sleep is not the same as "Sleep then hibernate after a specified time". Hybrid sleep simply becomes hibernate when power is lost, through battery running out. The behaviour described by the OP does not require Hybrid Sleep to be enabled.
– Paul
Dec 9 '11 at 2:51
add a comment |
In my research I found the same Linux Mint thread, but "Suspend Sedation" is not an official Microsoft term for that feature and as far as I can tell was invented by the Linux Mint forum user who mentioned it.
– ayan4m1
Nov 10 '10 at 20:43
Is there a better name for that feature?
– Sergey Stadnik
Nov 11 '10 at 2:01
As far as I can tell, there is no universally accepted name for the feature. "Hybrid suspend" is used by some, "suspend sedation" is used by that one Linux Mint forum user, and I've heard "hibernate and suspend" used to refer to the process before. Microsoft officially refers to it as "hybrid sleep," for Windows 7 at least.
– ayan4m1
Nov 11 '10 at 13:16
2
@ayan4m1 I realise this is an old question, but I think it is important to clarify this. Hyrbid sleep is not the same as "Sleep then hibernate after a specified time". Hybrid sleep simply becomes hibernate when power is lost, through battery running out. The behaviour described by the OP does not require Hybrid Sleep to be enabled.
– Paul
Dec 9 '11 at 2:51
In my research I found the same Linux Mint thread, but "Suspend Sedation" is not an official Microsoft term for that feature and as far as I can tell was invented by the Linux Mint forum user who mentioned it.
– ayan4m1
Nov 10 '10 at 20:43
In my research I found the same Linux Mint thread, but "Suspend Sedation" is not an official Microsoft term for that feature and as far as I can tell was invented by the Linux Mint forum user who mentioned it.
– ayan4m1
Nov 10 '10 at 20:43
Is there a better name for that feature?
– Sergey Stadnik
Nov 11 '10 at 2:01
Is there a better name for that feature?
– Sergey Stadnik
Nov 11 '10 at 2:01
As far as I can tell, there is no universally accepted name for the feature. "Hybrid suspend" is used by some, "suspend sedation" is used by that one Linux Mint forum user, and I've heard "hibernate and suspend" used to refer to the process before. Microsoft officially refers to it as "hybrid sleep," for Windows 7 at least.
– ayan4m1
Nov 11 '10 at 13:16
As far as I can tell, there is no universally accepted name for the feature. "Hybrid suspend" is used by some, "suspend sedation" is used by that one Linux Mint forum user, and I've heard "hibernate and suspend" used to refer to the process before. Microsoft officially refers to it as "hybrid sleep," for Windows 7 at least.
– ayan4m1
Nov 11 '10 at 13:16
2
2
@ayan4m1 I realise this is an old question, but I think it is important to clarify this. Hyrbid sleep is not the same as "Sleep then hibernate after a specified time". Hybrid sleep simply becomes hibernate when power is lost, through battery running out. The behaviour described by the OP does not require Hybrid Sleep to be enabled.
– Paul
Dec 9 '11 at 2:51
@ayan4m1 I realise this is an old question, but I think it is important to clarify this. Hyrbid sleep is not the same as "Sleep then hibernate after a specified time". Hybrid sleep simply becomes hibernate when power is lost, through battery running out. The behaviour described by the OP does not require Hybrid Sleep to be enabled.
– Paul
Dec 9 '11 at 2:51
add a comment |
10 Answers
10
active
oldest
votes
In Ubuntu 18.04 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.
This was spot on. Using it on Pop!_OS 18.10 (aka Ubuntu 18.10).
– eduncan911
Jan 27 at 4:44
Brilliant thank you! Does sleep.conf affect the hibernate mode in some way too, or does it only affect suspend-then-hibernate?
– user2428107
Feb 27 at 6:01
@user2428107 you can read about options in manual systutorials.com/docs/linux/man/5-systemd-sleep
– PRIHLOP
Feb 27 at 6:12
add a comment |
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.
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
.
It has worked here, but only after I chmoded the file to add X to everyone. I am a huge newbie and it took me 2 days to figure out. Very good script and I hope this helps whoever might be having problems. Thank you.
– user52343
Mar 29 '12 at 17:27
2
This would make a useful Ubuntu/Debian package!
– Petr Pudlák
Dec 10 '12 at 8:26
Just wondering: would this still be valid for Ubuntu 13.04? I need exactly this solution but I don't want to mess with wife's laptop if it turns out to break things on newer versions.
– Torben Gundtofte-Bruun
Sep 17 '13 at 6:33
Thanks for the script. Works fine for me on Ubuntu 14.04! One improvement would be if when the laptop wakes up to hibernate, it could check to see if it is plugged into AC power. If so, I would want it to suspend again instead of hibernating. Restoring from hibernate takes longer and I don't really need it to hibernate when it is plugged in...
– maddentim
Jun 6 '14 at 14:19
Thank you so much!!!! This script is magic I was dreaming of!!
– yanpas
Aug 10 '15 at 20:42
add a comment |
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"
1
The single apostrophe in your shell command could be misleading and confusing... please escape it.
– ayan4m1
Nov 11 '10 at 17:49
1
Bah, that's what happens when you edit a commandline embeded inside other text, without thinking about it as a commandline... Thanks & Fixed.
– JanC
Nov 12 '10 at 3:44
No problem, yeah understood about the different headspaces for the two processes.
– ayan4m1
Nov 12 '10 at 5:40
add a comment |
You may be interested in s2both. It is provided by the package uswsusp
in Ubuntu 10.10. It suspends to disk, but instead of shutting down the system instead puts it in S3, which is the power mode usually associated with the "Suspend" option in Ubuntu. pm-suspend-hybrid is another tool that purports to do the same thing.
To make this automated on lid close, take a look at the following guide which allows you to run an arbitrary script when a lid event is caught:
http://ubuntuforums.org/showthread.php?t=1076486
If you happen to have a ThinkPad, the manpage for tpctl
makes reference to an argument, --pm-sedation-hibernate-from-suspend-timer
, which seems to provide the feature you're looking for. I would caution you against trying this on non-ThinkPad hardware.
For reference, I looked through the manpage for hibernate.conf; it didn't seem to have any relevant options but might be worth a second reading.
add a comment |
Ubuntu 16.04 - from suspend/sleep into hibernate after a pre-determined time
It seems that on Ubuntu 16.04 things are a little different, so steps I took to make it work were:
Make sure hibernate is working as expected when running
systemctl hibernate
Copy the original
suspend.target
file:sudo cp /lib/systemd/system/suspend.target /etc/systemd/system/suspend.target
Then edit the file
/etc/systemd/system/suspend.target
and add the line:Requires=delayed-hibernation.service
to the
[Unit]
section of that file.Create the file
/etc/systemd/system/delayed-hibernation.service
with the following content:
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/delayed-hibernation.sh pre suspend
ExecStop=/usr/local/bin/delayed-hibernation.sh post suspend
[Install]
WantedBy=sleep.target
- Create the configuration file
/etc/delayed-hibernation.conf
for the script with the following content:
# Configuration file for 'delayed-hibernation.sh' script
# Specify the time in seconds to spend in sleep mode before the computer hibernates
TIMEOUT=1200 #in seconds, gives 20 minutes
Create the script which will actually does the hard work.
Create file
/usr/local/bin/delayed-hibernation.sh
with the content:
#!/bin/bash
# Script name: delayed-hibernation.sh
# Purpose: Auto hibernates after a period of sleep
# Edit the `TIMEOUT` variable in the `$hibernation_conf` file to set the number of seconds to sleep.
hibernation_lock='/var/run/delayed-hibernation.lock'
hibernation_fail='/var/run/delayed-hibernation.fail'
hibernation_conf='/etc/delayed-hibernation.conf'
# Checking the configuration file
if [ ! -f $hibernation_conf ]; then
echo "Missing configuration file ('$hibernation_conf'), aborting."
exit 1
fi
hibernation_timeout=$(grep "^[^#]" $hibernation_conf | grep "TIMEOUT=" | awk -F'=' ' print $2 ' | awk -F'#' 'print $1' | tr -d '[[ t]]')
if [ "$hibernation_timeout" = "" ]; then
echo "Missing 'TIMEOUT' parameter from configuration file ('$hibernation_conf'), aborting."
exit 1
elif [[ ! "$hibernation_timeout" =~ ^[0-9]+$ ]]; then
echo "Bad 'TIMEOUT' parameter ('$hibernation_timeout') in configuration file ('$hibernation_conf'), expected number of seconds, aborting."
exit 1
fi
# Processing given parameters
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
if [ -f $hibernation_fail ]; then
echo "Failed hibernation detected, skipping setting RTC wakeup timer."
else
echo "Suspend detected. Recording time, set RTC timer"
echo "$curtime" > $hibernation_lock
rtcwake -m no -s $hibernation_timeout
fi
elif [ "$1" = "post" ]; then
if [ -f $hibernation_fail ]; then
rm $hibernation_fail
fi
if [ -f $hibernation_lock ]; then
sustime=$(cat $hibernation_lock)
rm $hibernation_lock
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo "Automatic resume from suspend detected. Hibernating..."
systemctl hibernate
if [ $? -ne 0 ]; then
echo "Automatic hibernation failed. Trying to suspend instead."
touch $hibernation_fail
systemctl suspend
if [ $? -ne 0 ]; then
echo "Automatic hibernation and suspend failover failed. Nothing else to try."
fi
fi
else
echo "Manual resume from suspend detected. Clearing RTC timer"
rtcwake -m disable
fi
else
echo "File '$hibernation_lock' was not found, nothing to do"
fi
else
echo "Unrecognised first parameter: '$1', expected 'pre' or 'post'"
fi
else
echo "This script is intended to be run by systemctl delayed-hibernation.service (expected second parameter: 'suspend')"
fi
- Make the script executable:
chmod 755 /usr/local/bin/delayed-hibernation.sh
It took me quite a lot until writing this script based on other replies in this thread, things I found on the internet like https://bbs.archlinux.org/viewtopic.php?pid=1554259
My version of the script tries to deal with many problems like go into suspend again if hibernate was not successful but do not wake again after the pre-determined time over and over.
Final step I assume would be to just execute
sudo systemctl daemon-reload
sudo systemctl enable delayed-hibernation.serviceto make sure new service/configurations are being used.
To check the service log, you can use:
sudo systemctl status delayed-hibernation.service
or for a complete log of the service use:
sudo journalctl -u delayed-hibernation.service
A normal log I get from the running service is:
mile@mile-ThinkPad:~$ sudo systemctl status delayed-hibernation.service
● delayed-hibernation.service - Delayed hibernation trigger
Loaded: loaded (/etc/systemd/system/delayed-hibernation.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Jun 09 20:35:42 mile-ThinkPad systemd[1]: Starting Delayed hibernation trigger...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: Suspend detected. Recording time, set RTC timer
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: assuming RTC uses UTC ...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: wakeup using /dev/rtc0 at Thu Jun 9 18:55:43 2016
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Started Delayed hibernation trigger.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: delayed-hibernation.service: Unit not needed anymore. Stopping.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopping Delayed hibernation trigger...
Jun 09 20:55:44 mile-ThinkPad delayed-hibernation.sh[3093]: Automatic resume from suspend detected. Hibernating...
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopped Delayed hibernation trigger.
mile@mile-ThinkPad:~$
So This would be it, I hope it really helps someone since I spent days trying to figure out the right combination of configurations and script versions to make this handy feature work.
Thanks for the answer, this still works like a charm on Ubuntu 18.04. I couldn't get the above answers to work, executing/bin/systemctl hibernate
would always return 1 when running in the systemd script, even though it works fine on the command line.
– eugenhu
May 28 '18 at 6:21
add a comment |
Just in case something goes wrong during pm-hibernate
i'd rather put the computer to suspend than let it run. So you can use:
...
/usr/sbin/pm-hibernate || /usr/sbin/pm-suspend
...
add a comment |
Here's an updated version of Derek Pressnall's answer that works with systemd and includes Eliah Kagan's suggestion, just drop it in /usr/lib/systemd/system-sleep/delayed_hibernation.sh and make it executable:
#!/bin/bash
hibernation_timeout=1800 #30 minutes
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
echo -e "[($curtime) $@]nExecuting pre-suspend hook..." >> /tmp/delayed_hibernation.log
echo "$curtime" > /var/run/delayed_hibernation.lock
rtcwake -m no -s $hibernation_timeout
elif [ "$1" = "post" ]; then
echo -e "[($curtime) $@]nExecuting post-suspend hook..." >> /tmp/delayed_hibernation.log
sustime=$(cat /var/run/delayed_hibernation.lock)
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo -e "Automatic resume detected, hibernating.n" >> /tmp/delayed_hibernation.log
systemctl hibernate || systemctl suspend
else
echo -e "Manual resume detected, clearing RTC alarm.n" >> /tmp/delayed_hibernation.log
rtcwake -m no -s 1
fi
rm /var/run/delayed_hibernation.lock
fi
fi
This was working great for several months on 15.10 but something about 16.04 prevents it from hibernating even though the script still runs.
– Sean
Apr 23 '16 at 17:44
@Sean have you tried the workaround in this thread?
– Niccolò Maggioni
Apr 24 '16 at 11:00
Thanks for pointing me in the right direction. I created a systemd service (/etc/systemd/system/delayed-hibernation.service) that referenced the script above then modified /etc/systemd/system/suspend.target to require delayed-hibernation.service.
– Sean
Apr 24 '16 at 22:02
add a comment |
Here is my recipe (tested it on two notebooks Ubuntu 16.04):
Put this script whereever you like (I put it to root, /syspend.sh
) and make it executable (chmod +x /suspend.sh
)
TIMELOG=/tmp/autohibernate.log
ALARM=$(tail -n 1 $TIMELOG)
SLEEPTIME=5000 #edit this line to change timer, e.g. 2 hours "$((2*60*60))"
if [[ $1 == "resume" ]]
then
if [[ $(date +%s) -ge $(( $ALARM + $SLEEPTIME )) ]]
then
echo "hibernate triggered $(date +%H:%M:%S)">>$TIMELOG
systemctl hibernate 2>> $TIMELOG
else
echo "normal wakeup $(date +%H:%M:%S)">>$TIMELOG
fi
elif [[ $1 == "suspend" ]]
then
echo "$(date +%s)" >> $TIMELOG
rtcwake -m no -s $SLEEPTIME
fi
Then create systemd target:# touch /etc/systemd/system/suspend-to-sleep.target
Paste this content:
#/etc/systemd/system/suspend-to-hibernate.service
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash /suspend.sh suspend
ExecStop=/bin/bash /suspend.sh wakeup
[Install]
WantedBy=sleep.target
RequiredBy=suspend.target
Then enable it # systemctl enable suspend-to-sleep.target
.
I've faced an issue on the one of notebooks: closing lid didn't trigger this target. This was due to xfce4-power-manager. There are two ways to workaround this problem. The first one is to edit /etc/systemd/logind.conf
file and replace HandleLidSwitch=ignore
with HandleLidSwitch=suspend
. But it will be systemwide, so I just added symlink to my script # ln -s /suspend.sh /etc/pm/sleep.d/0000rtchibernate
add a comment |
Another more common workaround you can use hybrid-sleep
(like the Mac OS does). If your computer supports hibernation, you can use this feature:
systemctl hybrid-sleep
That command should suspend and send to disk (hibernate) the computer. After some time the computer will turn off (when turning on, it will use the hibernation files to wake up).
p.s.: I know it's not exactly what the OP posted, but it's fairly close
add a comment |
Don't forget to chmod +x that file, make it executable.
There's another solution without rtcwake, using wakealarm in /sys/class/rtc/rtc0. Make use obsolete code in pm-functions (/usr/lib/pm-utils) after the comments #since the kernel does not directly support ... , ('cos the current kernel (after 3.6 something) does directly support). Revert that code and put in do_suspend() part instead of do_suspend_hybrid().
Obsolete code (suspend then hibernate when suspend_hybrid is called):
# since the kernel does not directly support hybrid sleep, we do
# something else -- suspend and schedule an alarm to go into
# hibernate if we have slept long enough.
# Only do this if we do not need to do any special video hackery on resume
# from hibernate, though.
if [ -z "$SUSPEND_HYBRID_MODULE" -a -w "$PM_RTC/wakealarm" ] &&
check_suspend && check_hibernate && ! is_set $HIBERNATE_RESUME_POST_VIDEO;
then
SUSPEND_HYBRID_MODULE="kernel"
do_suspend_hybrid()
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend.
do_hibernate
fi
Recommended. Even easier to use uswsusp while the same time maximize the benefit of s2both i.e. s2both when suspend. Put the reverted code in do_suspend() part of uswsusp module (/usr/lib/pm-utils/module.d).
Reverted code (suspend_hybrid when suspend is called):
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend_hybrid; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend_hybrid.
do_hibernate || do_suspend_hybrid
else
echo > "$PM_RTC/wakealarm"
fi
else
# when do_suspend is being called, convert to suspend_hybrid.
do_suspend_hybrid
fi
With uswsusp, we can see the progress of suspend/hibernate and the reverse process displayed in text, even we can abort it by pressing backspace. Without uswsusp, suspend/hibernate just appear-disappear annoyingly, especially when wakealarm is triggered and execute hibernate (s2disk in uswsusp). Set the period of sleep before hibernate in the usual place on pm-functions file.
# variables to handle hibernate after suspend support
PM_HIBERNATE_DELAY=900 # 15 minutes
PM_RTC=/sys/class/rtc/rtc0
Here's the uswsusp mod: (remember, this module is called from pm-functions so the inserted variables are the same)
#!/bin/sh
# disable processing of 90chvt and 99video.
# s2ram and s2disk handle all this stuff internally.
uswsusp_hooks()
disablehook 99video "disabled by uswsusp"
# Since we disabled 99video, we need to take responsibility for proper
# quirk handling. s2ram handles all common video quirks internally,
# so all we have to do is translate the HAL standard options to s2ram options.
uswsusp_get_quirks()
OPTS=""
ACPI_SLEEP=0
for opt in $PM_CMDLINE; do
case "$opt##--quirk-" in # just quirks, please
dpms-on) ;; # no-op
dpms-suspend) ;; # no-op
radeon-off) OPTS="$OPTS --radeontool" ;;
reset-brightness) ;; # no-op
s3-bios) ACPI_SLEEP=$(($ACPI_SLEEP + 1)) ;;
s3-mode) ACPI_SLEEP=$(($ACPI_SLEEP + 2)) ;;
vbe-post) OPTS="$OPTS --vbe_post" ;;
vbemode-restore) OPTS="$OPTS --vbe_mode" ;;
vbestate-restore) OPTS="$OPTS --vbe_save" ;;
vga-mode-3) ;; # no-op
save-pci) OPTS="$OPTS --pci_save" ;;
none) QUIRK_NONE="true" ;;
*) continue ;;
esac
done
[ $ACPI_SLEEP -ne 0 ] && OPTS="$OPTS --acpi_sleep $ACPI_SLEEP"
# if we were told to ignore quirks, do so.
# This is arguably not the best way to do things, but...
[ "$QUIRK_NONE" = "true" ] && OPTS=""
# Since we disabled 99video, we also need to handle displaying
# help info for the quirks we handle.
uswsusp_help()
echo # first echo makes it look nicer.
echo "s2ram video quirk handler options:"
echo
echo " --quirk-radeon-off"
echo " --quirk-s3-bios"
echo " --quirk-s3-mode"
echo " --quirk-vbe-post"
echo " --quirk-vbemode-restore"
echo " --quirk-vbestate-restore"
echo " --quirk-save-pci"
echo " --quirk-none"
# This idiom is used for all sleep methods. Only declare the actual
# do_ method if:
# 1: some other sleep module has not already done so, and
# 2: this sleep method can actually work on this system.
#
# For suspend, if SUSPEND_MODULE is set then something else has already
# implemented do_suspend. We could just check to see of do_suspend was
# already declared using command_exists, but using a dedicated environment
# variable makes it easier to debug when we have to know what sleep module
# ended up claiming ownership of a given sleep method.
if [ -z "$SUSPEND_MODULE" ] && command_exists s2ram &&
( grep -q mem /sys/power/state ||
( [ -c /dev/pmu ] && check_suspend_pmu; ); ); then
SUSPEND_MODULE="uswsusp"
do_suspend()
fi
if [ -z "$HIBERNATE_MODULE" ] &&
[ -f /sys/power/disk ] &&
grep -q disk /sys/power/state &&
[ -c /dev/snapshot ] &&
command_exists s2disk; then
HIBERNATE_MODULE="uswsusp"
do_hibernate()
s2disk
fi
if [ -z "$SUSPEND_HYBRID_MODULE" ] &&
grep -q mem /sys/power/state &&
command_exists s2both &&
check_hibernate; then
SUSPEND_HYBRID_MODULE="uswsusp"
do_suspend_hybrid()
uswsusp_get_quirks
s2both --force $OPTS
if [ "$METHOD" = "suspend_hybrid" ]; then
add_before_hooks uswsusp_hooks
add_module_help uswsusp_help
fi
fi
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f12383%2fhow-to-go-automatically-from-suspend-into-hibernate%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
In Ubuntu 18.04 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.
This was spot on. Using it on Pop!_OS 18.10 (aka Ubuntu 18.10).
– eduncan911
Jan 27 at 4:44
Brilliant thank you! Does sleep.conf affect the hibernate mode in some way too, or does it only affect suspend-then-hibernate?
– user2428107
Feb 27 at 6:01
@user2428107 you can read about options in manual systutorials.com/docs/linux/man/5-systemd-sleep
– PRIHLOP
Feb 27 at 6:12
add a comment |
In Ubuntu 18.04 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.
This was spot on. Using it on Pop!_OS 18.10 (aka Ubuntu 18.10).
– eduncan911
Jan 27 at 4:44
Brilliant thank you! Does sleep.conf affect the hibernate mode in some way too, or does it only affect suspend-then-hibernate?
– user2428107
Feb 27 at 6:01
@user2428107 you can read about options in manual systutorials.com/docs/linux/man/5-systemd-sleep
– PRIHLOP
Feb 27 at 6:12
add a comment |
In Ubuntu 18.04 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.
In Ubuntu 18.04 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.
edited 10 hours ago
Pablo Bianchi
2,89521535
2,89521535
answered Sep 16 '18 at 22:11
PRIHLOPPRIHLOP
719612
719612
This was spot on. Using it on Pop!_OS 18.10 (aka Ubuntu 18.10).
– eduncan911
Jan 27 at 4:44
Brilliant thank you! Does sleep.conf affect the hibernate mode in some way too, or does it only affect suspend-then-hibernate?
– user2428107
Feb 27 at 6:01
@user2428107 you can read about options in manual systutorials.com/docs/linux/man/5-systemd-sleep
– PRIHLOP
Feb 27 at 6:12
add a comment |
This was spot on. Using it on Pop!_OS 18.10 (aka Ubuntu 18.10).
– eduncan911
Jan 27 at 4:44
Brilliant thank you! Does sleep.conf affect the hibernate mode in some way too, or does it only affect suspend-then-hibernate?
– user2428107
Feb 27 at 6:01
@user2428107 you can read about options in manual systutorials.com/docs/linux/man/5-systemd-sleep
– PRIHLOP
Feb 27 at 6:12
This was spot on. Using it on Pop!_OS 18.10 (aka Ubuntu 18.10).
– eduncan911
Jan 27 at 4:44
This was spot on. Using it on Pop!_OS 18.10 (aka Ubuntu 18.10).
– eduncan911
Jan 27 at 4:44
Brilliant thank you! Does sleep.conf affect the hibernate mode in some way too, or does it only affect suspend-then-hibernate?
– user2428107
Feb 27 at 6:01
Brilliant thank you! Does sleep.conf affect the hibernate mode in some way too, or does it only affect suspend-then-hibernate?
– user2428107
Feb 27 at 6:01
@user2428107 you can read about options in manual systutorials.com/docs/linux/man/5-systemd-sleep
– PRIHLOP
Feb 27 at 6:12
@user2428107 you can read about options in manual systutorials.com/docs/linux/man/5-systemd-sleep
– PRIHLOP
Feb 27 at 6:12
add a comment |
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.
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
.
It has worked here, but only after I chmoded the file to add X to everyone. I am a huge newbie and it took me 2 days to figure out. Very good script and I hope this helps whoever might be having problems. Thank you.
– user52343
Mar 29 '12 at 17:27
2
This would make a useful Ubuntu/Debian package!
– Petr Pudlák
Dec 10 '12 at 8:26
Just wondering: would this still be valid for Ubuntu 13.04? I need exactly this solution but I don't want to mess with wife's laptop if it turns out to break things on newer versions.
– Torben Gundtofte-Bruun
Sep 17 '13 at 6:33
Thanks for the script. Works fine for me on Ubuntu 14.04! One improvement would be if when the laptop wakes up to hibernate, it could check to see if it is plugged into AC power. If so, I would want it to suspend again instead of hibernating. Restoring from hibernate takes longer and I don't really need it to hibernate when it is plugged in...
– maddentim
Jun 6 '14 at 14:19
Thank you so much!!!! This script is magic I was dreaming of!!
– yanpas
Aug 10 '15 at 20:42
add a comment |
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.
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
.
It has worked here, but only after I chmoded the file to add X to everyone. I am a huge newbie and it took me 2 days to figure out. Very good script and I hope this helps whoever might be having problems. Thank you.
– user52343
Mar 29 '12 at 17:27
2
This would make a useful Ubuntu/Debian package!
– Petr Pudlák
Dec 10 '12 at 8:26
Just wondering: would this still be valid for Ubuntu 13.04? I need exactly this solution but I don't want to mess with wife's laptop if it turns out to break things on newer versions.
– Torben Gundtofte-Bruun
Sep 17 '13 at 6:33
Thanks for the script. Works fine for me on Ubuntu 14.04! One improvement would be if when the laptop wakes up to hibernate, it could check to see if it is plugged into AC power. If so, I would want it to suspend again instead of hibernating. Restoring from hibernate takes longer and I don't really need it to hibernate when it is plugged in...
– maddentim
Jun 6 '14 at 14:19
Thank you so much!!!! This script is magic I was dreaming of!!
– yanpas
Aug 10 '15 at 20:42
add a comment |
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.
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
.
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.
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
.
edited Dec 20 '17 at 21:36
dessert
24.3k670104
24.3k670104
answered Apr 2 '11 at 21:16
Derek PressnallDerek Pressnall
486145
486145
It has worked here, but only after I chmoded the file to add X to everyone. I am a huge newbie and it took me 2 days to figure out. Very good script and I hope this helps whoever might be having problems. Thank you.
– user52343
Mar 29 '12 at 17:27
2
This would make a useful Ubuntu/Debian package!
– Petr Pudlák
Dec 10 '12 at 8:26
Just wondering: would this still be valid for Ubuntu 13.04? I need exactly this solution but I don't want to mess with wife's laptop if it turns out to break things on newer versions.
– Torben Gundtofte-Bruun
Sep 17 '13 at 6:33
Thanks for the script. Works fine for me on Ubuntu 14.04! One improvement would be if when the laptop wakes up to hibernate, it could check to see if it is plugged into AC power. If so, I would want it to suspend again instead of hibernating. Restoring from hibernate takes longer and I don't really need it to hibernate when it is plugged in...
– maddentim
Jun 6 '14 at 14:19
Thank you so much!!!! This script is magic I was dreaming of!!
– yanpas
Aug 10 '15 at 20:42
add a comment |
It has worked here, but only after I chmoded the file to add X to everyone. I am a huge newbie and it took me 2 days to figure out. Very good script and I hope this helps whoever might be having problems. Thank you.
– user52343
Mar 29 '12 at 17:27
2
This would make a useful Ubuntu/Debian package!
– Petr Pudlák
Dec 10 '12 at 8:26
Just wondering: would this still be valid for Ubuntu 13.04? I need exactly this solution but I don't want to mess with wife's laptop if it turns out to break things on newer versions.
– Torben Gundtofte-Bruun
Sep 17 '13 at 6:33
Thanks for the script. Works fine for me on Ubuntu 14.04! One improvement would be if when the laptop wakes up to hibernate, it could check to see if it is plugged into AC power. If so, I would want it to suspend again instead of hibernating. Restoring from hibernate takes longer and I don't really need it to hibernate when it is plugged in...
– maddentim
Jun 6 '14 at 14:19
Thank you so much!!!! This script is magic I was dreaming of!!
– yanpas
Aug 10 '15 at 20:42
It has worked here, but only after I chmoded the file to add X to everyone. I am a huge newbie and it took me 2 days to figure out. Very good script and I hope this helps whoever might be having problems. Thank you.
– user52343
Mar 29 '12 at 17:27
It has worked here, but only after I chmoded the file to add X to everyone. I am a huge newbie and it took me 2 days to figure out. Very good script and I hope this helps whoever might be having problems. Thank you.
– user52343
Mar 29 '12 at 17:27
2
2
This would make a useful Ubuntu/Debian package!
– Petr Pudlák
Dec 10 '12 at 8:26
This would make a useful Ubuntu/Debian package!
– Petr Pudlák
Dec 10 '12 at 8:26
Just wondering: would this still be valid for Ubuntu 13.04? I need exactly this solution but I don't want to mess with wife's laptop if it turns out to break things on newer versions.
– Torben Gundtofte-Bruun
Sep 17 '13 at 6:33
Just wondering: would this still be valid for Ubuntu 13.04? I need exactly this solution but I don't want to mess with wife's laptop if it turns out to break things on newer versions.
– Torben Gundtofte-Bruun
Sep 17 '13 at 6:33
Thanks for the script. Works fine for me on Ubuntu 14.04! One improvement would be if when the laptop wakes up to hibernate, it could check to see if it is plugged into AC power. If so, I would want it to suspend again instead of hibernating. Restoring from hibernate takes longer and I don't really need it to hibernate when it is plugged in...
– maddentim
Jun 6 '14 at 14:19
Thanks for the script. Works fine for me on Ubuntu 14.04! One improvement would be if when the laptop wakes up to hibernate, it could check to see if it is plugged into AC power. If so, I would want it to suspend again instead of hibernating. Restoring from hibernate takes longer and I don't really need it to hibernate when it is plugged in...
– maddentim
Jun 6 '14 at 14:19
Thank you so much!!!! This script is magic I was dreaming of!!
– yanpas
Aug 10 '15 at 20:42
Thank you so much!!!! This script is magic I was dreaming of!!
– yanpas
Aug 10 '15 at 20:42
add a comment |
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"
1
The single apostrophe in your shell command could be misleading and confusing... please escape it.
– ayan4m1
Nov 11 '10 at 17:49
1
Bah, that's what happens when you edit a commandline embeded inside other text, without thinking about it as a commandline... Thanks & Fixed.
– JanC
Nov 12 '10 at 3:44
No problem, yeah understood about the different headspaces for the two processes.
– ayan4m1
Nov 12 '10 at 5:40
add a comment |
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"
1
The single apostrophe in your shell command could be misleading and confusing... please escape it.
– ayan4m1
Nov 11 '10 at 17:49
1
Bah, that's what happens when you edit a commandline embeded inside other text, without thinking about it as a commandline... Thanks & Fixed.
– JanC
Nov 12 '10 at 3:44
No problem, yeah understood about the different headspaces for the two processes.
– ayan4m1
Nov 12 '10 at 5:40
add a comment |
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"
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"
edited Nov 12 '10 at 3:41
answered Nov 10 '10 at 1:14
JanCJanC
16.8k13447
16.8k13447
1
The single apostrophe in your shell command could be misleading and confusing... please escape it.
– ayan4m1
Nov 11 '10 at 17:49
1
Bah, that's what happens when you edit a commandline embeded inside other text, without thinking about it as a commandline... Thanks & Fixed.
– JanC
Nov 12 '10 at 3:44
No problem, yeah understood about the different headspaces for the two processes.
– ayan4m1
Nov 12 '10 at 5:40
add a comment |
1
The single apostrophe in your shell command could be misleading and confusing... please escape it.
– ayan4m1
Nov 11 '10 at 17:49
1
Bah, that's what happens when you edit a commandline embeded inside other text, without thinking about it as a commandline... Thanks & Fixed.
– JanC
Nov 12 '10 at 3:44
No problem, yeah understood about the different headspaces for the two processes.
– ayan4m1
Nov 12 '10 at 5:40
1
1
The single apostrophe in your shell command could be misleading and confusing... please escape it.
– ayan4m1
Nov 11 '10 at 17:49
The single apostrophe in your shell command could be misleading and confusing... please escape it.
– ayan4m1
Nov 11 '10 at 17:49
1
1
Bah, that's what happens when you edit a commandline embeded inside other text, without thinking about it as a commandline... Thanks & Fixed.
– JanC
Nov 12 '10 at 3:44
Bah, that's what happens when you edit a commandline embeded inside other text, without thinking about it as a commandline... Thanks & Fixed.
– JanC
Nov 12 '10 at 3:44
No problem, yeah understood about the different headspaces for the two processes.
– ayan4m1
Nov 12 '10 at 5:40
No problem, yeah understood about the different headspaces for the two processes.
– ayan4m1
Nov 12 '10 at 5:40
add a comment |
You may be interested in s2both. It is provided by the package uswsusp
in Ubuntu 10.10. It suspends to disk, but instead of shutting down the system instead puts it in S3, which is the power mode usually associated with the "Suspend" option in Ubuntu. pm-suspend-hybrid is another tool that purports to do the same thing.
To make this automated on lid close, take a look at the following guide which allows you to run an arbitrary script when a lid event is caught:
http://ubuntuforums.org/showthread.php?t=1076486
If you happen to have a ThinkPad, the manpage for tpctl
makes reference to an argument, --pm-sedation-hibernate-from-suspend-timer
, which seems to provide the feature you're looking for. I would caution you against trying this on non-ThinkPad hardware.
For reference, I looked through the manpage for hibernate.conf; it didn't seem to have any relevant options but might be worth a second reading.
add a comment |
You may be interested in s2both. It is provided by the package uswsusp
in Ubuntu 10.10. It suspends to disk, but instead of shutting down the system instead puts it in S3, which is the power mode usually associated with the "Suspend" option in Ubuntu. pm-suspend-hybrid is another tool that purports to do the same thing.
To make this automated on lid close, take a look at the following guide which allows you to run an arbitrary script when a lid event is caught:
http://ubuntuforums.org/showthread.php?t=1076486
If you happen to have a ThinkPad, the manpage for tpctl
makes reference to an argument, --pm-sedation-hibernate-from-suspend-timer
, which seems to provide the feature you're looking for. I would caution you against trying this on non-ThinkPad hardware.
For reference, I looked through the manpage for hibernate.conf; it didn't seem to have any relevant options but might be worth a second reading.
add a comment |
You may be interested in s2both. It is provided by the package uswsusp
in Ubuntu 10.10. It suspends to disk, but instead of shutting down the system instead puts it in S3, which is the power mode usually associated with the "Suspend" option in Ubuntu. pm-suspend-hybrid is another tool that purports to do the same thing.
To make this automated on lid close, take a look at the following guide which allows you to run an arbitrary script when a lid event is caught:
http://ubuntuforums.org/showthread.php?t=1076486
If you happen to have a ThinkPad, the manpage for tpctl
makes reference to an argument, --pm-sedation-hibernate-from-suspend-timer
, which seems to provide the feature you're looking for. I would caution you against trying this on non-ThinkPad hardware.
For reference, I looked through the manpage for hibernate.conf; it didn't seem to have any relevant options but might be worth a second reading.
You may be interested in s2both. It is provided by the package uswsusp
in Ubuntu 10.10. It suspends to disk, but instead of shutting down the system instead puts it in S3, which is the power mode usually associated with the "Suspend" option in Ubuntu. pm-suspend-hybrid is another tool that purports to do the same thing.
To make this automated on lid close, take a look at the following guide which allows you to run an arbitrary script when a lid event is caught:
http://ubuntuforums.org/showthread.php?t=1076486
If you happen to have a ThinkPad, the manpage for tpctl
makes reference to an argument, --pm-sedation-hibernate-from-suspend-timer
, which seems to provide the feature you're looking for. I would caution you against trying this on non-ThinkPad hardware.
For reference, I looked through the manpage for hibernate.conf; it didn't seem to have any relevant options but might be worth a second reading.
edited Nov 10 '10 at 20:41
answered Nov 10 '10 at 1:21
ayan4m1ayan4m1
86369
86369
add a comment |
add a comment |
Ubuntu 16.04 - from suspend/sleep into hibernate after a pre-determined time
It seems that on Ubuntu 16.04 things are a little different, so steps I took to make it work were:
Make sure hibernate is working as expected when running
systemctl hibernate
Copy the original
suspend.target
file:sudo cp /lib/systemd/system/suspend.target /etc/systemd/system/suspend.target
Then edit the file
/etc/systemd/system/suspend.target
and add the line:Requires=delayed-hibernation.service
to the
[Unit]
section of that file.Create the file
/etc/systemd/system/delayed-hibernation.service
with the following content:
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/delayed-hibernation.sh pre suspend
ExecStop=/usr/local/bin/delayed-hibernation.sh post suspend
[Install]
WantedBy=sleep.target
- Create the configuration file
/etc/delayed-hibernation.conf
for the script with the following content:
# Configuration file for 'delayed-hibernation.sh' script
# Specify the time in seconds to spend in sleep mode before the computer hibernates
TIMEOUT=1200 #in seconds, gives 20 minutes
Create the script which will actually does the hard work.
Create file
/usr/local/bin/delayed-hibernation.sh
with the content:
#!/bin/bash
# Script name: delayed-hibernation.sh
# Purpose: Auto hibernates after a period of sleep
# Edit the `TIMEOUT` variable in the `$hibernation_conf` file to set the number of seconds to sleep.
hibernation_lock='/var/run/delayed-hibernation.lock'
hibernation_fail='/var/run/delayed-hibernation.fail'
hibernation_conf='/etc/delayed-hibernation.conf'
# Checking the configuration file
if [ ! -f $hibernation_conf ]; then
echo "Missing configuration file ('$hibernation_conf'), aborting."
exit 1
fi
hibernation_timeout=$(grep "^[^#]" $hibernation_conf | grep "TIMEOUT=" | awk -F'=' ' print $2 ' | awk -F'#' 'print $1' | tr -d '[[ t]]')
if [ "$hibernation_timeout" = "" ]; then
echo "Missing 'TIMEOUT' parameter from configuration file ('$hibernation_conf'), aborting."
exit 1
elif [[ ! "$hibernation_timeout" =~ ^[0-9]+$ ]]; then
echo "Bad 'TIMEOUT' parameter ('$hibernation_timeout') in configuration file ('$hibernation_conf'), expected number of seconds, aborting."
exit 1
fi
# Processing given parameters
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
if [ -f $hibernation_fail ]; then
echo "Failed hibernation detected, skipping setting RTC wakeup timer."
else
echo "Suspend detected. Recording time, set RTC timer"
echo "$curtime" > $hibernation_lock
rtcwake -m no -s $hibernation_timeout
fi
elif [ "$1" = "post" ]; then
if [ -f $hibernation_fail ]; then
rm $hibernation_fail
fi
if [ -f $hibernation_lock ]; then
sustime=$(cat $hibernation_lock)
rm $hibernation_lock
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo "Automatic resume from suspend detected. Hibernating..."
systemctl hibernate
if [ $? -ne 0 ]; then
echo "Automatic hibernation failed. Trying to suspend instead."
touch $hibernation_fail
systemctl suspend
if [ $? -ne 0 ]; then
echo "Automatic hibernation and suspend failover failed. Nothing else to try."
fi
fi
else
echo "Manual resume from suspend detected. Clearing RTC timer"
rtcwake -m disable
fi
else
echo "File '$hibernation_lock' was not found, nothing to do"
fi
else
echo "Unrecognised first parameter: '$1', expected 'pre' or 'post'"
fi
else
echo "This script is intended to be run by systemctl delayed-hibernation.service (expected second parameter: 'suspend')"
fi
- Make the script executable:
chmod 755 /usr/local/bin/delayed-hibernation.sh
It took me quite a lot until writing this script based on other replies in this thread, things I found on the internet like https://bbs.archlinux.org/viewtopic.php?pid=1554259
My version of the script tries to deal with many problems like go into suspend again if hibernate was not successful but do not wake again after the pre-determined time over and over.
Final step I assume would be to just execute
sudo systemctl daemon-reload
sudo systemctl enable delayed-hibernation.serviceto make sure new service/configurations are being used.
To check the service log, you can use:
sudo systemctl status delayed-hibernation.service
or for a complete log of the service use:
sudo journalctl -u delayed-hibernation.service
A normal log I get from the running service is:
mile@mile-ThinkPad:~$ sudo systemctl status delayed-hibernation.service
● delayed-hibernation.service - Delayed hibernation trigger
Loaded: loaded (/etc/systemd/system/delayed-hibernation.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Jun 09 20:35:42 mile-ThinkPad systemd[1]: Starting Delayed hibernation trigger...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: Suspend detected. Recording time, set RTC timer
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: assuming RTC uses UTC ...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: wakeup using /dev/rtc0 at Thu Jun 9 18:55:43 2016
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Started Delayed hibernation trigger.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: delayed-hibernation.service: Unit not needed anymore. Stopping.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopping Delayed hibernation trigger...
Jun 09 20:55:44 mile-ThinkPad delayed-hibernation.sh[3093]: Automatic resume from suspend detected. Hibernating...
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopped Delayed hibernation trigger.
mile@mile-ThinkPad:~$
So This would be it, I hope it really helps someone since I spent days trying to figure out the right combination of configurations and script versions to make this handy feature work.
Thanks for the answer, this still works like a charm on Ubuntu 18.04. I couldn't get the above answers to work, executing/bin/systemctl hibernate
would always return 1 when running in the systemd script, even though it works fine on the command line.
– eugenhu
May 28 '18 at 6:21
add a comment |
Ubuntu 16.04 - from suspend/sleep into hibernate after a pre-determined time
It seems that on Ubuntu 16.04 things are a little different, so steps I took to make it work were:
Make sure hibernate is working as expected when running
systemctl hibernate
Copy the original
suspend.target
file:sudo cp /lib/systemd/system/suspend.target /etc/systemd/system/suspend.target
Then edit the file
/etc/systemd/system/suspend.target
and add the line:Requires=delayed-hibernation.service
to the
[Unit]
section of that file.Create the file
/etc/systemd/system/delayed-hibernation.service
with the following content:
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/delayed-hibernation.sh pre suspend
ExecStop=/usr/local/bin/delayed-hibernation.sh post suspend
[Install]
WantedBy=sleep.target
- Create the configuration file
/etc/delayed-hibernation.conf
for the script with the following content:
# Configuration file for 'delayed-hibernation.sh' script
# Specify the time in seconds to spend in sleep mode before the computer hibernates
TIMEOUT=1200 #in seconds, gives 20 minutes
Create the script which will actually does the hard work.
Create file
/usr/local/bin/delayed-hibernation.sh
with the content:
#!/bin/bash
# Script name: delayed-hibernation.sh
# Purpose: Auto hibernates after a period of sleep
# Edit the `TIMEOUT` variable in the `$hibernation_conf` file to set the number of seconds to sleep.
hibernation_lock='/var/run/delayed-hibernation.lock'
hibernation_fail='/var/run/delayed-hibernation.fail'
hibernation_conf='/etc/delayed-hibernation.conf'
# Checking the configuration file
if [ ! -f $hibernation_conf ]; then
echo "Missing configuration file ('$hibernation_conf'), aborting."
exit 1
fi
hibernation_timeout=$(grep "^[^#]" $hibernation_conf | grep "TIMEOUT=" | awk -F'=' ' print $2 ' | awk -F'#' 'print $1' | tr -d '[[ t]]')
if [ "$hibernation_timeout" = "" ]; then
echo "Missing 'TIMEOUT' parameter from configuration file ('$hibernation_conf'), aborting."
exit 1
elif [[ ! "$hibernation_timeout" =~ ^[0-9]+$ ]]; then
echo "Bad 'TIMEOUT' parameter ('$hibernation_timeout') in configuration file ('$hibernation_conf'), expected number of seconds, aborting."
exit 1
fi
# Processing given parameters
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
if [ -f $hibernation_fail ]; then
echo "Failed hibernation detected, skipping setting RTC wakeup timer."
else
echo "Suspend detected. Recording time, set RTC timer"
echo "$curtime" > $hibernation_lock
rtcwake -m no -s $hibernation_timeout
fi
elif [ "$1" = "post" ]; then
if [ -f $hibernation_fail ]; then
rm $hibernation_fail
fi
if [ -f $hibernation_lock ]; then
sustime=$(cat $hibernation_lock)
rm $hibernation_lock
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo "Automatic resume from suspend detected. Hibernating..."
systemctl hibernate
if [ $? -ne 0 ]; then
echo "Automatic hibernation failed. Trying to suspend instead."
touch $hibernation_fail
systemctl suspend
if [ $? -ne 0 ]; then
echo "Automatic hibernation and suspend failover failed. Nothing else to try."
fi
fi
else
echo "Manual resume from suspend detected. Clearing RTC timer"
rtcwake -m disable
fi
else
echo "File '$hibernation_lock' was not found, nothing to do"
fi
else
echo "Unrecognised first parameter: '$1', expected 'pre' or 'post'"
fi
else
echo "This script is intended to be run by systemctl delayed-hibernation.service (expected second parameter: 'suspend')"
fi
- Make the script executable:
chmod 755 /usr/local/bin/delayed-hibernation.sh
It took me quite a lot until writing this script based on other replies in this thread, things I found on the internet like https://bbs.archlinux.org/viewtopic.php?pid=1554259
My version of the script tries to deal with many problems like go into suspend again if hibernate was not successful but do not wake again after the pre-determined time over and over.
Final step I assume would be to just execute
sudo systemctl daemon-reload
sudo systemctl enable delayed-hibernation.serviceto make sure new service/configurations are being used.
To check the service log, you can use:
sudo systemctl status delayed-hibernation.service
or for a complete log of the service use:
sudo journalctl -u delayed-hibernation.service
A normal log I get from the running service is:
mile@mile-ThinkPad:~$ sudo systemctl status delayed-hibernation.service
● delayed-hibernation.service - Delayed hibernation trigger
Loaded: loaded (/etc/systemd/system/delayed-hibernation.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Jun 09 20:35:42 mile-ThinkPad systemd[1]: Starting Delayed hibernation trigger...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: Suspend detected. Recording time, set RTC timer
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: assuming RTC uses UTC ...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: wakeup using /dev/rtc0 at Thu Jun 9 18:55:43 2016
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Started Delayed hibernation trigger.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: delayed-hibernation.service: Unit not needed anymore. Stopping.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopping Delayed hibernation trigger...
Jun 09 20:55:44 mile-ThinkPad delayed-hibernation.sh[3093]: Automatic resume from suspend detected. Hibernating...
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopped Delayed hibernation trigger.
mile@mile-ThinkPad:~$
So This would be it, I hope it really helps someone since I spent days trying to figure out the right combination of configurations and script versions to make this handy feature work.
Thanks for the answer, this still works like a charm on Ubuntu 18.04. I couldn't get the above answers to work, executing/bin/systemctl hibernate
would always return 1 when running in the systemd script, even though it works fine on the command line.
– eugenhu
May 28 '18 at 6:21
add a comment |
Ubuntu 16.04 - from suspend/sleep into hibernate after a pre-determined time
It seems that on Ubuntu 16.04 things are a little different, so steps I took to make it work were:
Make sure hibernate is working as expected when running
systemctl hibernate
Copy the original
suspend.target
file:sudo cp /lib/systemd/system/suspend.target /etc/systemd/system/suspend.target
Then edit the file
/etc/systemd/system/suspend.target
and add the line:Requires=delayed-hibernation.service
to the
[Unit]
section of that file.Create the file
/etc/systemd/system/delayed-hibernation.service
with the following content:
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/delayed-hibernation.sh pre suspend
ExecStop=/usr/local/bin/delayed-hibernation.sh post suspend
[Install]
WantedBy=sleep.target
- Create the configuration file
/etc/delayed-hibernation.conf
for the script with the following content:
# Configuration file for 'delayed-hibernation.sh' script
# Specify the time in seconds to spend in sleep mode before the computer hibernates
TIMEOUT=1200 #in seconds, gives 20 minutes
Create the script which will actually does the hard work.
Create file
/usr/local/bin/delayed-hibernation.sh
with the content:
#!/bin/bash
# Script name: delayed-hibernation.sh
# Purpose: Auto hibernates after a period of sleep
# Edit the `TIMEOUT` variable in the `$hibernation_conf` file to set the number of seconds to sleep.
hibernation_lock='/var/run/delayed-hibernation.lock'
hibernation_fail='/var/run/delayed-hibernation.fail'
hibernation_conf='/etc/delayed-hibernation.conf'
# Checking the configuration file
if [ ! -f $hibernation_conf ]; then
echo "Missing configuration file ('$hibernation_conf'), aborting."
exit 1
fi
hibernation_timeout=$(grep "^[^#]" $hibernation_conf | grep "TIMEOUT=" | awk -F'=' ' print $2 ' | awk -F'#' 'print $1' | tr -d '[[ t]]')
if [ "$hibernation_timeout" = "" ]; then
echo "Missing 'TIMEOUT' parameter from configuration file ('$hibernation_conf'), aborting."
exit 1
elif [[ ! "$hibernation_timeout" =~ ^[0-9]+$ ]]; then
echo "Bad 'TIMEOUT' parameter ('$hibernation_timeout') in configuration file ('$hibernation_conf'), expected number of seconds, aborting."
exit 1
fi
# Processing given parameters
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
if [ -f $hibernation_fail ]; then
echo "Failed hibernation detected, skipping setting RTC wakeup timer."
else
echo "Suspend detected. Recording time, set RTC timer"
echo "$curtime" > $hibernation_lock
rtcwake -m no -s $hibernation_timeout
fi
elif [ "$1" = "post" ]; then
if [ -f $hibernation_fail ]; then
rm $hibernation_fail
fi
if [ -f $hibernation_lock ]; then
sustime=$(cat $hibernation_lock)
rm $hibernation_lock
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo "Automatic resume from suspend detected. Hibernating..."
systemctl hibernate
if [ $? -ne 0 ]; then
echo "Automatic hibernation failed. Trying to suspend instead."
touch $hibernation_fail
systemctl suspend
if [ $? -ne 0 ]; then
echo "Automatic hibernation and suspend failover failed. Nothing else to try."
fi
fi
else
echo "Manual resume from suspend detected. Clearing RTC timer"
rtcwake -m disable
fi
else
echo "File '$hibernation_lock' was not found, nothing to do"
fi
else
echo "Unrecognised first parameter: '$1', expected 'pre' or 'post'"
fi
else
echo "This script is intended to be run by systemctl delayed-hibernation.service (expected second parameter: 'suspend')"
fi
- Make the script executable:
chmod 755 /usr/local/bin/delayed-hibernation.sh
It took me quite a lot until writing this script based on other replies in this thread, things I found on the internet like https://bbs.archlinux.org/viewtopic.php?pid=1554259
My version of the script tries to deal with many problems like go into suspend again if hibernate was not successful but do not wake again after the pre-determined time over and over.
Final step I assume would be to just execute
sudo systemctl daemon-reload
sudo systemctl enable delayed-hibernation.serviceto make sure new service/configurations are being used.
To check the service log, you can use:
sudo systemctl status delayed-hibernation.service
or for a complete log of the service use:
sudo journalctl -u delayed-hibernation.service
A normal log I get from the running service is:
mile@mile-ThinkPad:~$ sudo systemctl status delayed-hibernation.service
● delayed-hibernation.service - Delayed hibernation trigger
Loaded: loaded (/etc/systemd/system/delayed-hibernation.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Jun 09 20:35:42 mile-ThinkPad systemd[1]: Starting Delayed hibernation trigger...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: Suspend detected. Recording time, set RTC timer
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: assuming RTC uses UTC ...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: wakeup using /dev/rtc0 at Thu Jun 9 18:55:43 2016
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Started Delayed hibernation trigger.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: delayed-hibernation.service: Unit not needed anymore. Stopping.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopping Delayed hibernation trigger...
Jun 09 20:55:44 mile-ThinkPad delayed-hibernation.sh[3093]: Automatic resume from suspend detected. Hibernating...
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopped Delayed hibernation trigger.
mile@mile-ThinkPad:~$
So This would be it, I hope it really helps someone since I spent days trying to figure out the right combination of configurations and script versions to make this handy feature work.
Ubuntu 16.04 - from suspend/sleep into hibernate after a pre-determined time
It seems that on Ubuntu 16.04 things are a little different, so steps I took to make it work were:
Make sure hibernate is working as expected when running
systemctl hibernate
Copy the original
suspend.target
file:sudo cp /lib/systemd/system/suspend.target /etc/systemd/system/suspend.target
Then edit the file
/etc/systemd/system/suspend.target
and add the line:Requires=delayed-hibernation.service
to the
[Unit]
section of that file.Create the file
/etc/systemd/system/delayed-hibernation.service
with the following content:
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/delayed-hibernation.sh pre suspend
ExecStop=/usr/local/bin/delayed-hibernation.sh post suspend
[Install]
WantedBy=sleep.target
- Create the configuration file
/etc/delayed-hibernation.conf
for the script with the following content:
# Configuration file for 'delayed-hibernation.sh' script
# Specify the time in seconds to spend in sleep mode before the computer hibernates
TIMEOUT=1200 #in seconds, gives 20 minutes
Create the script which will actually does the hard work.
Create file
/usr/local/bin/delayed-hibernation.sh
with the content:
#!/bin/bash
# Script name: delayed-hibernation.sh
# Purpose: Auto hibernates after a period of sleep
# Edit the `TIMEOUT` variable in the `$hibernation_conf` file to set the number of seconds to sleep.
hibernation_lock='/var/run/delayed-hibernation.lock'
hibernation_fail='/var/run/delayed-hibernation.fail'
hibernation_conf='/etc/delayed-hibernation.conf'
# Checking the configuration file
if [ ! -f $hibernation_conf ]; then
echo "Missing configuration file ('$hibernation_conf'), aborting."
exit 1
fi
hibernation_timeout=$(grep "^[^#]" $hibernation_conf | grep "TIMEOUT=" | awk -F'=' ' print $2 ' | awk -F'#' 'print $1' | tr -d '[[ t]]')
if [ "$hibernation_timeout" = "" ]; then
echo "Missing 'TIMEOUT' parameter from configuration file ('$hibernation_conf'), aborting."
exit 1
elif [[ ! "$hibernation_timeout" =~ ^[0-9]+$ ]]; then
echo "Bad 'TIMEOUT' parameter ('$hibernation_timeout') in configuration file ('$hibernation_conf'), expected number of seconds, aborting."
exit 1
fi
# Processing given parameters
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
if [ -f $hibernation_fail ]; then
echo "Failed hibernation detected, skipping setting RTC wakeup timer."
else
echo "Suspend detected. Recording time, set RTC timer"
echo "$curtime" > $hibernation_lock
rtcwake -m no -s $hibernation_timeout
fi
elif [ "$1" = "post" ]; then
if [ -f $hibernation_fail ]; then
rm $hibernation_fail
fi
if [ -f $hibernation_lock ]; then
sustime=$(cat $hibernation_lock)
rm $hibernation_lock
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo "Automatic resume from suspend detected. Hibernating..."
systemctl hibernate
if [ $? -ne 0 ]; then
echo "Automatic hibernation failed. Trying to suspend instead."
touch $hibernation_fail
systemctl suspend
if [ $? -ne 0 ]; then
echo "Automatic hibernation and suspend failover failed. Nothing else to try."
fi
fi
else
echo "Manual resume from suspend detected. Clearing RTC timer"
rtcwake -m disable
fi
else
echo "File '$hibernation_lock' was not found, nothing to do"
fi
else
echo "Unrecognised first parameter: '$1', expected 'pre' or 'post'"
fi
else
echo "This script is intended to be run by systemctl delayed-hibernation.service (expected second parameter: 'suspend')"
fi
- Make the script executable:
chmod 755 /usr/local/bin/delayed-hibernation.sh
It took me quite a lot until writing this script based on other replies in this thread, things I found on the internet like https://bbs.archlinux.org/viewtopic.php?pid=1554259
My version of the script tries to deal with many problems like go into suspend again if hibernate was not successful but do not wake again after the pre-determined time over and over.
Final step I assume would be to just execute
sudo systemctl daemon-reload
sudo systemctl enable delayed-hibernation.serviceto make sure new service/configurations are being used.
To check the service log, you can use:
sudo systemctl status delayed-hibernation.service
or for a complete log of the service use:
sudo journalctl -u delayed-hibernation.service
A normal log I get from the running service is:
mile@mile-ThinkPad:~$ sudo systemctl status delayed-hibernation.service
● delayed-hibernation.service - Delayed hibernation trigger
Loaded: loaded (/etc/systemd/system/delayed-hibernation.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Jun 09 20:35:42 mile-ThinkPad systemd[1]: Starting Delayed hibernation trigger...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: Suspend detected. Recording time, set RTC timer
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: assuming RTC uses UTC ...
Jun 09 20:35:42 mile-ThinkPad delayed-hibernation.sh[2933]: rtcwake: wakeup using /dev/rtc0 at Thu Jun 9 18:55:43 2016
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Started Delayed hibernation trigger.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: delayed-hibernation.service: Unit not needed anymore. Stopping.
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopping Delayed hibernation trigger...
Jun 09 20:55:44 mile-ThinkPad delayed-hibernation.sh[3093]: Automatic resume from suspend detected. Hibernating...
Jun 09 20:55:44 mile-ThinkPad systemd[1]: Stopped Delayed hibernation trigger.
mile@mile-ThinkPad:~$
So This would be it, I hope it really helps someone since I spent days trying to figure out the right combination of configurations and script versions to make this handy feature work.
edited Aug 10 '16 at 4:55
Kevin Keegan
31
31
answered Jun 10 '16 at 17:41
mihai.ilemihai.ile
5112
5112
Thanks for the answer, this still works like a charm on Ubuntu 18.04. I couldn't get the above answers to work, executing/bin/systemctl hibernate
would always return 1 when running in the systemd script, even though it works fine on the command line.
– eugenhu
May 28 '18 at 6:21
add a comment |
Thanks for the answer, this still works like a charm on Ubuntu 18.04. I couldn't get the above answers to work, executing/bin/systemctl hibernate
would always return 1 when running in the systemd script, even though it works fine on the command line.
– eugenhu
May 28 '18 at 6:21
Thanks for the answer, this still works like a charm on Ubuntu 18.04. I couldn't get the above answers to work, executing
/bin/systemctl hibernate
would always return 1 when running in the systemd script, even though it works fine on the command line.– eugenhu
May 28 '18 at 6:21
Thanks for the answer, this still works like a charm on Ubuntu 18.04. I couldn't get the above answers to work, executing
/bin/systemctl hibernate
would always return 1 when running in the systemd script, even though it works fine on the command line.– eugenhu
May 28 '18 at 6:21
add a comment |
Just in case something goes wrong during pm-hibernate
i'd rather put the computer to suspend than let it run. So you can use:
...
/usr/sbin/pm-hibernate || /usr/sbin/pm-suspend
...
add a comment |
Just in case something goes wrong during pm-hibernate
i'd rather put the computer to suspend than let it run. So you can use:
...
/usr/sbin/pm-hibernate || /usr/sbin/pm-suspend
...
add a comment |
Just in case something goes wrong during pm-hibernate
i'd rather put the computer to suspend than let it run. So you can use:
...
/usr/sbin/pm-hibernate || /usr/sbin/pm-suspend
...
Just in case something goes wrong during pm-hibernate
i'd rather put the computer to suspend than let it run. So you can use:
...
/usr/sbin/pm-hibernate || /usr/sbin/pm-suspend
...
edited Feb 28 '13 at 22:29
Eliah Kagan
82.6k22227369
82.6k22227369
answered Feb 28 '13 at 21:24
iiegniiegn
411
411
add a comment |
add a comment |
Here's an updated version of Derek Pressnall's answer that works with systemd and includes Eliah Kagan's suggestion, just drop it in /usr/lib/systemd/system-sleep/delayed_hibernation.sh and make it executable:
#!/bin/bash
hibernation_timeout=1800 #30 minutes
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
echo -e "[($curtime) $@]nExecuting pre-suspend hook..." >> /tmp/delayed_hibernation.log
echo "$curtime" > /var/run/delayed_hibernation.lock
rtcwake -m no -s $hibernation_timeout
elif [ "$1" = "post" ]; then
echo -e "[($curtime) $@]nExecuting post-suspend hook..." >> /tmp/delayed_hibernation.log
sustime=$(cat /var/run/delayed_hibernation.lock)
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo -e "Automatic resume detected, hibernating.n" >> /tmp/delayed_hibernation.log
systemctl hibernate || systemctl suspend
else
echo -e "Manual resume detected, clearing RTC alarm.n" >> /tmp/delayed_hibernation.log
rtcwake -m no -s 1
fi
rm /var/run/delayed_hibernation.lock
fi
fi
This was working great for several months on 15.10 but something about 16.04 prevents it from hibernating even though the script still runs.
– Sean
Apr 23 '16 at 17:44
@Sean have you tried the workaround in this thread?
– Niccolò Maggioni
Apr 24 '16 at 11:00
Thanks for pointing me in the right direction. I created a systemd service (/etc/systemd/system/delayed-hibernation.service) that referenced the script above then modified /etc/systemd/system/suspend.target to require delayed-hibernation.service.
– Sean
Apr 24 '16 at 22:02
add a comment |
Here's an updated version of Derek Pressnall's answer that works with systemd and includes Eliah Kagan's suggestion, just drop it in /usr/lib/systemd/system-sleep/delayed_hibernation.sh and make it executable:
#!/bin/bash
hibernation_timeout=1800 #30 minutes
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
echo -e "[($curtime) $@]nExecuting pre-suspend hook..." >> /tmp/delayed_hibernation.log
echo "$curtime" > /var/run/delayed_hibernation.lock
rtcwake -m no -s $hibernation_timeout
elif [ "$1" = "post" ]; then
echo -e "[($curtime) $@]nExecuting post-suspend hook..." >> /tmp/delayed_hibernation.log
sustime=$(cat /var/run/delayed_hibernation.lock)
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo -e "Automatic resume detected, hibernating.n" >> /tmp/delayed_hibernation.log
systemctl hibernate || systemctl suspend
else
echo -e "Manual resume detected, clearing RTC alarm.n" >> /tmp/delayed_hibernation.log
rtcwake -m no -s 1
fi
rm /var/run/delayed_hibernation.lock
fi
fi
This was working great for several months on 15.10 but something about 16.04 prevents it from hibernating even though the script still runs.
– Sean
Apr 23 '16 at 17:44
@Sean have you tried the workaround in this thread?
– Niccolò Maggioni
Apr 24 '16 at 11:00
Thanks for pointing me in the right direction. I created a systemd service (/etc/systemd/system/delayed-hibernation.service) that referenced the script above then modified /etc/systemd/system/suspend.target to require delayed-hibernation.service.
– Sean
Apr 24 '16 at 22:02
add a comment |
Here's an updated version of Derek Pressnall's answer that works with systemd and includes Eliah Kagan's suggestion, just drop it in /usr/lib/systemd/system-sleep/delayed_hibernation.sh and make it executable:
#!/bin/bash
hibernation_timeout=1800 #30 minutes
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
echo -e "[($curtime) $@]nExecuting pre-suspend hook..." >> /tmp/delayed_hibernation.log
echo "$curtime" > /var/run/delayed_hibernation.lock
rtcwake -m no -s $hibernation_timeout
elif [ "$1" = "post" ]; then
echo -e "[($curtime) $@]nExecuting post-suspend hook..." >> /tmp/delayed_hibernation.log
sustime=$(cat /var/run/delayed_hibernation.lock)
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo -e "Automatic resume detected, hibernating.n" >> /tmp/delayed_hibernation.log
systemctl hibernate || systemctl suspend
else
echo -e "Manual resume detected, clearing RTC alarm.n" >> /tmp/delayed_hibernation.log
rtcwake -m no -s 1
fi
rm /var/run/delayed_hibernation.lock
fi
fi
Here's an updated version of Derek Pressnall's answer that works with systemd and includes Eliah Kagan's suggestion, just drop it in /usr/lib/systemd/system-sleep/delayed_hibernation.sh and make it executable:
#!/bin/bash
hibernation_timeout=1800 #30 minutes
if [ "$2" = "suspend" ]; then
curtime=$(date +%s)
if [ "$1" = "pre" ]; then
echo -e "[($curtime) $@]nExecuting pre-suspend hook..." >> /tmp/delayed_hibernation.log
echo "$curtime" > /var/run/delayed_hibernation.lock
rtcwake -m no -s $hibernation_timeout
elif [ "$1" = "post" ]; then
echo -e "[($curtime) $@]nExecuting post-suspend hook..." >> /tmp/delayed_hibernation.log
sustime=$(cat /var/run/delayed_hibernation.lock)
if [ $(($curtime - $sustime)) -ge $hibernation_timeout ]; then
echo -e "Automatic resume detected, hibernating.n" >> /tmp/delayed_hibernation.log
systemctl hibernate || systemctl suspend
else
echo -e "Manual resume detected, clearing RTC alarm.n" >> /tmp/delayed_hibernation.log
rtcwake -m no -s 1
fi
rm /var/run/delayed_hibernation.lock
fi
fi
edited Apr 13 '17 at 12:24
Community♦
1
1
answered Aug 17 '15 at 21:13
Niccolò MaggioniNiccolò Maggioni
335
335
This was working great for several months on 15.10 but something about 16.04 prevents it from hibernating even though the script still runs.
– Sean
Apr 23 '16 at 17:44
@Sean have you tried the workaround in this thread?
– Niccolò Maggioni
Apr 24 '16 at 11:00
Thanks for pointing me in the right direction. I created a systemd service (/etc/systemd/system/delayed-hibernation.service) that referenced the script above then modified /etc/systemd/system/suspend.target to require delayed-hibernation.service.
– Sean
Apr 24 '16 at 22:02
add a comment |
This was working great for several months on 15.10 but something about 16.04 prevents it from hibernating even though the script still runs.
– Sean
Apr 23 '16 at 17:44
@Sean have you tried the workaround in this thread?
– Niccolò Maggioni
Apr 24 '16 at 11:00
Thanks for pointing me in the right direction. I created a systemd service (/etc/systemd/system/delayed-hibernation.service) that referenced the script above then modified /etc/systemd/system/suspend.target to require delayed-hibernation.service.
– Sean
Apr 24 '16 at 22:02
This was working great for several months on 15.10 but something about 16.04 prevents it from hibernating even though the script still runs.
– Sean
Apr 23 '16 at 17:44
This was working great for several months on 15.10 but something about 16.04 prevents it from hibernating even though the script still runs.
– Sean
Apr 23 '16 at 17:44
@Sean have you tried the workaround in this thread?
– Niccolò Maggioni
Apr 24 '16 at 11:00
@Sean have you tried the workaround in this thread?
– Niccolò Maggioni
Apr 24 '16 at 11:00
Thanks for pointing me in the right direction. I created a systemd service (/etc/systemd/system/delayed-hibernation.service) that referenced the script above then modified /etc/systemd/system/suspend.target to require delayed-hibernation.service.
– Sean
Apr 24 '16 at 22:02
Thanks for pointing me in the right direction. I created a systemd service (/etc/systemd/system/delayed-hibernation.service) that referenced the script above then modified /etc/systemd/system/suspend.target to require delayed-hibernation.service.
– Sean
Apr 24 '16 at 22:02
add a comment |
Here is my recipe (tested it on two notebooks Ubuntu 16.04):
Put this script whereever you like (I put it to root, /syspend.sh
) and make it executable (chmod +x /suspend.sh
)
TIMELOG=/tmp/autohibernate.log
ALARM=$(tail -n 1 $TIMELOG)
SLEEPTIME=5000 #edit this line to change timer, e.g. 2 hours "$((2*60*60))"
if [[ $1 == "resume" ]]
then
if [[ $(date +%s) -ge $(( $ALARM + $SLEEPTIME )) ]]
then
echo "hibernate triggered $(date +%H:%M:%S)">>$TIMELOG
systemctl hibernate 2>> $TIMELOG
else
echo "normal wakeup $(date +%H:%M:%S)">>$TIMELOG
fi
elif [[ $1 == "suspend" ]]
then
echo "$(date +%s)" >> $TIMELOG
rtcwake -m no -s $SLEEPTIME
fi
Then create systemd target:# touch /etc/systemd/system/suspend-to-sleep.target
Paste this content:
#/etc/systemd/system/suspend-to-hibernate.service
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash /suspend.sh suspend
ExecStop=/bin/bash /suspend.sh wakeup
[Install]
WantedBy=sleep.target
RequiredBy=suspend.target
Then enable it # systemctl enable suspend-to-sleep.target
.
I've faced an issue on the one of notebooks: closing lid didn't trigger this target. This was due to xfce4-power-manager. There are two ways to workaround this problem. The first one is to edit /etc/systemd/logind.conf
file and replace HandleLidSwitch=ignore
with HandleLidSwitch=suspend
. But it will be systemwide, so I just added symlink to my script # ln -s /suspend.sh /etc/pm/sleep.d/0000rtchibernate
add a comment |
Here is my recipe (tested it on two notebooks Ubuntu 16.04):
Put this script whereever you like (I put it to root, /syspend.sh
) and make it executable (chmod +x /suspend.sh
)
TIMELOG=/tmp/autohibernate.log
ALARM=$(tail -n 1 $TIMELOG)
SLEEPTIME=5000 #edit this line to change timer, e.g. 2 hours "$((2*60*60))"
if [[ $1 == "resume" ]]
then
if [[ $(date +%s) -ge $(( $ALARM + $SLEEPTIME )) ]]
then
echo "hibernate triggered $(date +%H:%M:%S)">>$TIMELOG
systemctl hibernate 2>> $TIMELOG
else
echo "normal wakeup $(date +%H:%M:%S)">>$TIMELOG
fi
elif [[ $1 == "suspend" ]]
then
echo "$(date +%s)" >> $TIMELOG
rtcwake -m no -s $SLEEPTIME
fi
Then create systemd target:# touch /etc/systemd/system/suspend-to-sleep.target
Paste this content:
#/etc/systemd/system/suspend-to-hibernate.service
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash /suspend.sh suspend
ExecStop=/bin/bash /suspend.sh wakeup
[Install]
WantedBy=sleep.target
RequiredBy=suspend.target
Then enable it # systemctl enable suspend-to-sleep.target
.
I've faced an issue on the one of notebooks: closing lid didn't trigger this target. This was due to xfce4-power-manager. There are two ways to workaround this problem. The first one is to edit /etc/systemd/logind.conf
file and replace HandleLidSwitch=ignore
with HandleLidSwitch=suspend
. But it will be systemwide, so I just added symlink to my script # ln -s /suspend.sh /etc/pm/sleep.d/0000rtchibernate
add a comment |
Here is my recipe (tested it on two notebooks Ubuntu 16.04):
Put this script whereever you like (I put it to root, /syspend.sh
) and make it executable (chmod +x /suspend.sh
)
TIMELOG=/tmp/autohibernate.log
ALARM=$(tail -n 1 $TIMELOG)
SLEEPTIME=5000 #edit this line to change timer, e.g. 2 hours "$((2*60*60))"
if [[ $1 == "resume" ]]
then
if [[ $(date +%s) -ge $(( $ALARM + $SLEEPTIME )) ]]
then
echo "hibernate triggered $(date +%H:%M:%S)">>$TIMELOG
systemctl hibernate 2>> $TIMELOG
else
echo "normal wakeup $(date +%H:%M:%S)">>$TIMELOG
fi
elif [[ $1 == "suspend" ]]
then
echo "$(date +%s)" >> $TIMELOG
rtcwake -m no -s $SLEEPTIME
fi
Then create systemd target:# touch /etc/systemd/system/suspend-to-sleep.target
Paste this content:
#/etc/systemd/system/suspend-to-hibernate.service
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash /suspend.sh suspend
ExecStop=/bin/bash /suspend.sh wakeup
[Install]
WantedBy=sleep.target
RequiredBy=suspend.target
Then enable it # systemctl enable suspend-to-sleep.target
.
I've faced an issue on the one of notebooks: closing lid didn't trigger this target. This was due to xfce4-power-manager. There are two ways to workaround this problem. The first one is to edit /etc/systemd/logind.conf
file and replace HandleLidSwitch=ignore
with HandleLidSwitch=suspend
. But it will be systemwide, so I just added symlink to my script # ln -s /suspend.sh /etc/pm/sleep.d/0000rtchibernate
Here is my recipe (tested it on two notebooks Ubuntu 16.04):
Put this script whereever you like (I put it to root, /syspend.sh
) and make it executable (chmod +x /suspend.sh
)
TIMELOG=/tmp/autohibernate.log
ALARM=$(tail -n 1 $TIMELOG)
SLEEPTIME=5000 #edit this line to change timer, e.g. 2 hours "$((2*60*60))"
if [[ $1 == "resume" ]]
then
if [[ $(date +%s) -ge $(( $ALARM + $SLEEPTIME )) ]]
then
echo "hibernate triggered $(date +%H:%M:%S)">>$TIMELOG
systemctl hibernate 2>> $TIMELOG
else
echo "normal wakeup $(date +%H:%M:%S)">>$TIMELOG
fi
elif [[ $1 == "suspend" ]]
then
echo "$(date +%s)" >> $TIMELOG
rtcwake -m no -s $SLEEPTIME
fi
Then create systemd target:# touch /etc/systemd/system/suspend-to-sleep.target
Paste this content:
#/etc/systemd/system/suspend-to-hibernate.service
[Unit]
Description=Delayed hibernation trigger
Before=suspend.target
Conflicts=hibernate.target hybrid-suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash /suspend.sh suspend
ExecStop=/bin/bash /suspend.sh wakeup
[Install]
WantedBy=sleep.target
RequiredBy=suspend.target
Then enable it # systemctl enable suspend-to-sleep.target
.
I've faced an issue on the one of notebooks: closing lid didn't trigger this target. This was due to xfce4-power-manager. There are two ways to workaround this problem. The first one is to edit /etc/systemd/logind.conf
file and replace HandleLidSwitch=ignore
with HandleLidSwitch=suspend
. But it will be systemwide, so I just added symlink to my script # ln -s /suspend.sh /etc/pm/sleep.d/0000rtchibernate
answered Oct 4 '16 at 19:39
yanpasyanpas
403314
403314
add a comment |
add a comment |
Another more common workaround you can use hybrid-sleep
(like the Mac OS does). If your computer supports hibernation, you can use this feature:
systemctl hybrid-sleep
That command should suspend and send to disk (hibernate) the computer. After some time the computer will turn off (when turning on, it will use the hibernation files to wake up).
p.s.: I know it's not exactly what the OP posted, but it's fairly close
add a comment |
Another more common workaround you can use hybrid-sleep
(like the Mac OS does). If your computer supports hibernation, you can use this feature:
systemctl hybrid-sleep
That command should suspend and send to disk (hibernate) the computer. After some time the computer will turn off (when turning on, it will use the hibernation files to wake up).
p.s.: I know it's not exactly what the OP posted, but it's fairly close
add a comment |
Another more common workaround you can use hybrid-sleep
(like the Mac OS does). If your computer supports hibernation, you can use this feature:
systemctl hybrid-sleep
That command should suspend and send to disk (hibernate) the computer. After some time the computer will turn off (when turning on, it will use the hibernation files to wake up).
p.s.: I know it's not exactly what the OP posted, but it's fairly close
Another more common workaround you can use hybrid-sleep
(like the Mac OS does). If your computer supports hibernation, you can use this feature:
systemctl hybrid-sleep
That command should suspend and send to disk (hibernate) the computer. After some time the computer will turn off (when turning on, it will use the hibernation files to wake up).
p.s.: I know it's not exactly what the OP posted, but it's fairly close
answered Jul 10 '17 at 10:58
morhookmorhook
1,0471013
1,0471013
add a comment |
add a comment |
Don't forget to chmod +x that file, make it executable.
There's another solution without rtcwake, using wakealarm in /sys/class/rtc/rtc0. Make use obsolete code in pm-functions (/usr/lib/pm-utils) after the comments #since the kernel does not directly support ... , ('cos the current kernel (after 3.6 something) does directly support). Revert that code and put in do_suspend() part instead of do_suspend_hybrid().
Obsolete code (suspend then hibernate when suspend_hybrid is called):
# since the kernel does not directly support hybrid sleep, we do
# something else -- suspend and schedule an alarm to go into
# hibernate if we have slept long enough.
# Only do this if we do not need to do any special video hackery on resume
# from hibernate, though.
if [ -z "$SUSPEND_HYBRID_MODULE" -a -w "$PM_RTC/wakealarm" ] &&
check_suspend && check_hibernate && ! is_set $HIBERNATE_RESUME_POST_VIDEO;
then
SUSPEND_HYBRID_MODULE="kernel"
do_suspend_hybrid()
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend.
do_hibernate
fi
Recommended. Even easier to use uswsusp while the same time maximize the benefit of s2both i.e. s2both when suspend. Put the reverted code in do_suspend() part of uswsusp module (/usr/lib/pm-utils/module.d).
Reverted code (suspend_hybrid when suspend is called):
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend_hybrid; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend_hybrid.
do_hibernate || do_suspend_hybrid
else
echo > "$PM_RTC/wakealarm"
fi
else
# when do_suspend is being called, convert to suspend_hybrid.
do_suspend_hybrid
fi
With uswsusp, we can see the progress of suspend/hibernate and the reverse process displayed in text, even we can abort it by pressing backspace. Without uswsusp, suspend/hibernate just appear-disappear annoyingly, especially when wakealarm is triggered and execute hibernate (s2disk in uswsusp). Set the period of sleep before hibernate in the usual place on pm-functions file.
# variables to handle hibernate after suspend support
PM_HIBERNATE_DELAY=900 # 15 minutes
PM_RTC=/sys/class/rtc/rtc0
Here's the uswsusp mod: (remember, this module is called from pm-functions so the inserted variables are the same)
#!/bin/sh
# disable processing of 90chvt and 99video.
# s2ram and s2disk handle all this stuff internally.
uswsusp_hooks()
disablehook 99video "disabled by uswsusp"
# Since we disabled 99video, we need to take responsibility for proper
# quirk handling. s2ram handles all common video quirks internally,
# so all we have to do is translate the HAL standard options to s2ram options.
uswsusp_get_quirks()
OPTS=""
ACPI_SLEEP=0
for opt in $PM_CMDLINE; do
case "$opt##--quirk-" in # just quirks, please
dpms-on) ;; # no-op
dpms-suspend) ;; # no-op
radeon-off) OPTS="$OPTS --radeontool" ;;
reset-brightness) ;; # no-op
s3-bios) ACPI_SLEEP=$(($ACPI_SLEEP + 1)) ;;
s3-mode) ACPI_SLEEP=$(($ACPI_SLEEP + 2)) ;;
vbe-post) OPTS="$OPTS --vbe_post" ;;
vbemode-restore) OPTS="$OPTS --vbe_mode" ;;
vbestate-restore) OPTS="$OPTS --vbe_save" ;;
vga-mode-3) ;; # no-op
save-pci) OPTS="$OPTS --pci_save" ;;
none) QUIRK_NONE="true" ;;
*) continue ;;
esac
done
[ $ACPI_SLEEP -ne 0 ] && OPTS="$OPTS --acpi_sleep $ACPI_SLEEP"
# if we were told to ignore quirks, do so.
# This is arguably not the best way to do things, but...
[ "$QUIRK_NONE" = "true" ] && OPTS=""
# Since we disabled 99video, we also need to handle displaying
# help info for the quirks we handle.
uswsusp_help()
echo # first echo makes it look nicer.
echo "s2ram video quirk handler options:"
echo
echo " --quirk-radeon-off"
echo " --quirk-s3-bios"
echo " --quirk-s3-mode"
echo " --quirk-vbe-post"
echo " --quirk-vbemode-restore"
echo " --quirk-vbestate-restore"
echo " --quirk-save-pci"
echo " --quirk-none"
# This idiom is used for all sleep methods. Only declare the actual
# do_ method if:
# 1: some other sleep module has not already done so, and
# 2: this sleep method can actually work on this system.
#
# For suspend, if SUSPEND_MODULE is set then something else has already
# implemented do_suspend. We could just check to see of do_suspend was
# already declared using command_exists, but using a dedicated environment
# variable makes it easier to debug when we have to know what sleep module
# ended up claiming ownership of a given sleep method.
if [ -z "$SUSPEND_MODULE" ] && command_exists s2ram &&
( grep -q mem /sys/power/state ||
( [ -c /dev/pmu ] && check_suspend_pmu; ); ); then
SUSPEND_MODULE="uswsusp"
do_suspend()
fi
if [ -z "$HIBERNATE_MODULE" ] &&
[ -f /sys/power/disk ] &&
grep -q disk /sys/power/state &&
[ -c /dev/snapshot ] &&
command_exists s2disk; then
HIBERNATE_MODULE="uswsusp"
do_hibernate()
s2disk
fi
if [ -z "$SUSPEND_HYBRID_MODULE" ] &&
grep -q mem /sys/power/state &&
command_exists s2both &&
check_hibernate; then
SUSPEND_HYBRID_MODULE="uswsusp"
do_suspend_hybrid()
uswsusp_get_quirks
s2both --force $OPTS
if [ "$METHOD" = "suspend_hybrid" ]; then
add_before_hooks uswsusp_hooks
add_module_help uswsusp_help
fi
fi
add a comment |
Don't forget to chmod +x that file, make it executable.
There's another solution without rtcwake, using wakealarm in /sys/class/rtc/rtc0. Make use obsolete code in pm-functions (/usr/lib/pm-utils) after the comments #since the kernel does not directly support ... , ('cos the current kernel (after 3.6 something) does directly support). Revert that code and put in do_suspend() part instead of do_suspend_hybrid().
Obsolete code (suspend then hibernate when suspend_hybrid is called):
# since the kernel does not directly support hybrid sleep, we do
# something else -- suspend and schedule an alarm to go into
# hibernate if we have slept long enough.
# Only do this if we do not need to do any special video hackery on resume
# from hibernate, though.
if [ -z "$SUSPEND_HYBRID_MODULE" -a -w "$PM_RTC/wakealarm" ] &&
check_suspend && check_hibernate && ! is_set $HIBERNATE_RESUME_POST_VIDEO;
then
SUSPEND_HYBRID_MODULE="kernel"
do_suspend_hybrid()
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend.
do_hibernate
fi
Recommended. Even easier to use uswsusp while the same time maximize the benefit of s2both i.e. s2both when suspend. Put the reverted code in do_suspend() part of uswsusp module (/usr/lib/pm-utils/module.d).
Reverted code (suspend_hybrid when suspend is called):
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend_hybrid; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend_hybrid.
do_hibernate || do_suspend_hybrid
else
echo > "$PM_RTC/wakealarm"
fi
else
# when do_suspend is being called, convert to suspend_hybrid.
do_suspend_hybrid
fi
With uswsusp, we can see the progress of suspend/hibernate and the reverse process displayed in text, even we can abort it by pressing backspace. Without uswsusp, suspend/hibernate just appear-disappear annoyingly, especially when wakealarm is triggered and execute hibernate (s2disk in uswsusp). Set the period of sleep before hibernate in the usual place on pm-functions file.
# variables to handle hibernate after suspend support
PM_HIBERNATE_DELAY=900 # 15 minutes
PM_RTC=/sys/class/rtc/rtc0
Here's the uswsusp mod: (remember, this module is called from pm-functions so the inserted variables are the same)
#!/bin/sh
# disable processing of 90chvt and 99video.
# s2ram and s2disk handle all this stuff internally.
uswsusp_hooks()
disablehook 99video "disabled by uswsusp"
# Since we disabled 99video, we need to take responsibility for proper
# quirk handling. s2ram handles all common video quirks internally,
# so all we have to do is translate the HAL standard options to s2ram options.
uswsusp_get_quirks()
OPTS=""
ACPI_SLEEP=0
for opt in $PM_CMDLINE; do
case "$opt##--quirk-" in # just quirks, please
dpms-on) ;; # no-op
dpms-suspend) ;; # no-op
radeon-off) OPTS="$OPTS --radeontool" ;;
reset-brightness) ;; # no-op
s3-bios) ACPI_SLEEP=$(($ACPI_SLEEP + 1)) ;;
s3-mode) ACPI_SLEEP=$(($ACPI_SLEEP + 2)) ;;
vbe-post) OPTS="$OPTS --vbe_post" ;;
vbemode-restore) OPTS="$OPTS --vbe_mode" ;;
vbestate-restore) OPTS="$OPTS --vbe_save" ;;
vga-mode-3) ;; # no-op
save-pci) OPTS="$OPTS --pci_save" ;;
none) QUIRK_NONE="true" ;;
*) continue ;;
esac
done
[ $ACPI_SLEEP -ne 0 ] && OPTS="$OPTS --acpi_sleep $ACPI_SLEEP"
# if we were told to ignore quirks, do so.
# This is arguably not the best way to do things, but...
[ "$QUIRK_NONE" = "true" ] && OPTS=""
# Since we disabled 99video, we also need to handle displaying
# help info for the quirks we handle.
uswsusp_help()
echo # first echo makes it look nicer.
echo "s2ram video quirk handler options:"
echo
echo " --quirk-radeon-off"
echo " --quirk-s3-bios"
echo " --quirk-s3-mode"
echo " --quirk-vbe-post"
echo " --quirk-vbemode-restore"
echo " --quirk-vbestate-restore"
echo " --quirk-save-pci"
echo " --quirk-none"
# This idiom is used for all sleep methods. Only declare the actual
# do_ method if:
# 1: some other sleep module has not already done so, and
# 2: this sleep method can actually work on this system.
#
# For suspend, if SUSPEND_MODULE is set then something else has already
# implemented do_suspend. We could just check to see of do_suspend was
# already declared using command_exists, but using a dedicated environment
# variable makes it easier to debug when we have to know what sleep module
# ended up claiming ownership of a given sleep method.
if [ -z "$SUSPEND_MODULE" ] && command_exists s2ram &&
( grep -q mem /sys/power/state ||
( [ -c /dev/pmu ] && check_suspend_pmu; ); ); then
SUSPEND_MODULE="uswsusp"
do_suspend()
fi
if [ -z "$HIBERNATE_MODULE" ] &&
[ -f /sys/power/disk ] &&
grep -q disk /sys/power/state &&
[ -c /dev/snapshot ] &&
command_exists s2disk; then
HIBERNATE_MODULE="uswsusp"
do_hibernate()
s2disk
fi
if [ -z "$SUSPEND_HYBRID_MODULE" ] &&
grep -q mem /sys/power/state &&
command_exists s2both &&
check_hibernate; then
SUSPEND_HYBRID_MODULE="uswsusp"
do_suspend_hybrid()
uswsusp_get_quirks
s2both --force $OPTS
if [ "$METHOD" = "suspend_hybrid" ]; then
add_before_hooks uswsusp_hooks
add_module_help uswsusp_help
fi
fi
add a comment |
Don't forget to chmod +x that file, make it executable.
There's another solution without rtcwake, using wakealarm in /sys/class/rtc/rtc0. Make use obsolete code in pm-functions (/usr/lib/pm-utils) after the comments #since the kernel does not directly support ... , ('cos the current kernel (after 3.6 something) does directly support). Revert that code and put in do_suspend() part instead of do_suspend_hybrid().
Obsolete code (suspend then hibernate when suspend_hybrid is called):
# since the kernel does not directly support hybrid sleep, we do
# something else -- suspend and schedule an alarm to go into
# hibernate if we have slept long enough.
# Only do this if we do not need to do any special video hackery on resume
# from hibernate, though.
if [ -z "$SUSPEND_HYBRID_MODULE" -a -w "$PM_RTC/wakealarm" ] &&
check_suspend && check_hibernate && ! is_set $HIBERNATE_RESUME_POST_VIDEO;
then
SUSPEND_HYBRID_MODULE="kernel"
do_suspend_hybrid()
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend.
do_hibernate
fi
Recommended. Even easier to use uswsusp while the same time maximize the benefit of s2both i.e. s2both when suspend. Put the reverted code in do_suspend() part of uswsusp module (/usr/lib/pm-utils/module.d).
Reverted code (suspend_hybrid when suspend is called):
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend_hybrid; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend_hybrid.
do_hibernate || do_suspend_hybrid
else
echo > "$PM_RTC/wakealarm"
fi
else
# when do_suspend is being called, convert to suspend_hybrid.
do_suspend_hybrid
fi
With uswsusp, we can see the progress of suspend/hibernate and the reverse process displayed in text, even we can abort it by pressing backspace. Without uswsusp, suspend/hibernate just appear-disappear annoyingly, especially when wakealarm is triggered and execute hibernate (s2disk in uswsusp). Set the period of sleep before hibernate in the usual place on pm-functions file.
# variables to handle hibernate after suspend support
PM_HIBERNATE_DELAY=900 # 15 minutes
PM_RTC=/sys/class/rtc/rtc0
Here's the uswsusp mod: (remember, this module is called from pm-functions so the inserted variables are the same)
#!/bin/sh
# disable processing of 90chvt and 99video.
# s2ram and s2disk handle all this stuff internally.
uswsusp_hooks()
disablehook 99video "disabled by uswsusp"
# Since we disabled 99video, we need to take responsibility for proper
# quirk handling. s2ram handles all common video quirks internally,
# so all we have to do is translate the HAL standard options to s2ram options.
uswsusp_get_quirks()
OPTS=""
ACPI_SLEEP=0
for opt in $PM_CMDLINE; do
case "$opt##--quirk-" in # just quirks, please
dpms-on) ;; # no-op
dpms-suspend) ;; # no-op
radeon-off) OPTS="$OPTS --radeontool" ;;
reset-brightness) ;; # no-op
s3-bios) ACPI_SLEEP=$(($ACPI_SLEEP + 1)) ;;
s3-mode) ACPI_SLEEP=$(($ACPI_SLEEP + 2)) ;;
vbe-post) OPTS="$OPTS --vbe_post" ;;
vbemode-restore) OPTS="$OPTS --vbe_mode" ;;
vbestate-restore) OPTS="$OPTS --vbe_save" ;;
vga-mode-3) ;; # no-op
save-pci) OPTS="$OPTS --pci_save" ;;
none) QUIRK_NONE="true" ;;
*) continue ;;
esac
done
[ $ACPI_SLEEP -ne 0 ] && OPTS="$OPTS --acpi_sleep $ACPI_SLEEP"
# if we were told to ignore quirks, do so.
# This is arguably not the best way to do things, but...
[ "$QUIRK_NONE" = "true" ] && OPTS=""
# Since we disabled 99video, we also need to handle displaying
# help info for the quirks we handle.
uswsusp_help()
echo # first echo makes it look nicer.
echo "s2ram video quirk handler options:"
echo
echo " --quirk-radeon-off"
echo " --quirk-s3-bios"
echo " --quirk-s3-mode"
echo " --quirk-vbe-post"
echo " --quirk-vbemode-restore"
echo " --quirk-vbestate-restore"
echo " --quirk-save-pci"
echo " --quirk-none"
# This idiom is used for all sleep methods. Only declare the actual
# do_ method if:
# 1: some other sleep module has not already done so, and
# 2: this sleep method can actually work on this system.
#
# For suspend, if SUSPEND_MODULE is set then something else has already
# implemented do_suspend. We could just check to see of do_suspend was
# already declared using command_exists, but using a dedicated environment
# variable makes it easier to debug when we have to know what sleep module
# ended up claiming ownership of a given sleep method.
if [ -z "$SUSPEND_MODULE" ] && command_exists s2ram &&
( grep -q mem /sys/power/state ||
( [ -c /dev/pmu ] && check_suspend_pmu; ); ); then
SUSPEND_MODULE="uswsusp"
do_suspend()
fi
if [ -z "$HIBERNATE_MODULE" ] &&
[ -f /sys/power/disk ] &&
grep -q disk /sys/power/state &&
[ -c /dev/snapshot ] &&
command_exists s2disk; then
HIBERNATE_MODULE="uswsusp"
do_hibernate()
s2disk
fi
if [ -z "$SUSPEND_HYBRID_MODULE" ] &&
grep -q mem /sys/power/state &&
command_exists s2both &&
check_hibernate; then
SUSPEND_HYBRID_MODULE="uswsusp"
do_suspend_hybrid()
uswsusp_get_quirks
s2both --force $OPTS
if [ "$METHOD" = "suspend_hybrid" ]; then
add_before_hooks uswsusp_hooks
add_module_help uswsusp_help
fi
fi
Don't forget to chmod +x that file, make it executable.
There's another solution without rtcwake, using wakealarm in /sys/class/rtc/rtc0. Make use obsolete code in pm-functions (/usr/lib/pm-utils) after the comments #since the kernel does not directly support ... , ('cos the current kernel (after 3.6 something) does directly support). Revert that code and put in do_suspend() part instead of do_suspend_hybrid().
Obsolete code (suspend then hibernate when suspend_hybrid is called):
# since the kernel does not directly support hybrid sleep, we do
# something else -- suspend and schedule an alarm to go into
# hibernate if we have slept long enough.
# Only do this if we do not need to do any special video hackery on resume
# from hibernate, though.
if [ -z "$SUSPEND_HYBRID_MODULE" -a -w "$PM_RTC/wakealarm" ] &&
check_suspend && check_hibernate && ! is_set $HIBERNATE_RESUME_POST_VIDEO;
then
SUSPEND_HYBRID_MODULE="kernel"
do_suspend_hybrid()
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend.
do_hibernate
fi
Recommended. Even easier to use uswsusp while the same time maximize the benefit of s2both i.e. s2both when suspend. Put the reverted code in do_suspend() part of uswsusp module (/usr/lib/pm-utils/module.d).
Reverted code (suspend_hybrid when suspend is called):
WAKETIME=$(( $(cat "$PM_RTC/since_epoch") + PM_HIBERNATE_DELAY))
echo >"$PM_RTC/wakealarm"
echo $WAKETIME > "$PM_RTC/wakealarm"
if do_suspend_hybrid; then
NOW=$(cat "$PM_RTC/since_epoch")
if [ "$NOW" -ge "$WAKETIME" -a "$NOW" -lt $((WAKETIME + 30)) ]; then
log "Woken by RTC alarm, hibernating."
# if hibernate fails for any reason, go back to suspend_hybrid.
do_hibernate || do_suspend_hybrid
else
echo > "$PM_RTC/wakealarm"
fi
else
# when do_suspend is being called, convert to suspend_hybrid.
do_suspend_hybrid
fi
With uswsusp, we can see the progress of suspend/hibernate and the reverse process displayed in text, even we can abort it by pressing backspace. Without uswsusp, suspend/hibernate just appear-disappear annoyingly, especially when wakealarm is triggered and execute hibernate (s2disk in uswsusp). Set the period of sleep before hibernate in the usual place on pm-functions file.
# variables to handle hibernate after suspend support
PM_HIBERNATE_DELAY=900 # 15 minutes
PM_RTC=/sys/class/rtc/rtc0
Here's the uswsusp mod: (remember, this module is called from pm-functions so the inserted variables are the same)
#!/bin/sh
# disable processing of 90chvt and 99video.
# s2ram and s2disk handle all this stuff internally.
uswsusp_hooks()
disablehook 99video "disabled by uswsusp"
# Since we disabled 99video, we need to take responsibility for proper
# quirk handling. s2ram handles all common video quirks internally,
# so all we have to do is translate the HAL standard options to s2ram options.
uswsusp_get_quirks()
OPTS=""
ACPI_SLEEP=0
for opt in $PM_CMDLINE; do
case "$opt##--quirk-" in # just quirks, please
dpms-on) ;; # no-op
dpms-suspend) ;; # no-op
radeon-off) OPTS="$OPTS --radeontool" ;;
reset-brightness) ;; # no-op
s3-bios) ACPI_SLEEP=$(($ACPI_SLEEP + 1)) ;;
s3-mode) ACPI_SLEEP=$(($ACPI_SLEEP + 2)) ;;
vbe-post) OPTS="$OPTS --vbe_post" ;;
vbemode-restore) OPTS="$OPTS --vbe_mode" ;;
vbestate-restore) OPTS="$OPTS --vbe_save" ;;
vga-mode-3) ;; # no-op
save-pci) OPTS="$OPTS --pci_save" ;;
none) QUIRK_NONE="true" ;;
*) continue ;;
esac
done
[ $ACPI_SLEEP -ne 0 ] && OPTS="$OPTS --acpi_sleep $ACPI_SLEEP"
# if we were told to ignore quirks, do so.
# This is arguably not the best way to do things, but...
[ "$QUIRK_NONE" = "true" ] && OPTS=""
# Since we disabled 99video, we also need to handle displaying
# help info for the quirks we handle.
uswsusp_help()
echo # first echo makes it look nicer.
echo "s2ram video quirk handler options:"
echo
echo " --quirk-radeon-off"
echo " --quirk-s3-bios"
echo " --quirk-s3-mode"
echo " --quirk-vbe-post"
echo " --quirk-vbemode-restore"
echo " --quirk-vbestate-restore"
echo " --quirk-save-pci"
echo " --quirk-none"
# This idiom is used for all sleep methods. Only declare the actual
# do_ method if:
# 1: some other sleep module has not already done so, and
# 2: this sleep method can actually work on this system.
#
# For suspend, if SUSPEND_MODULE is set then something else has already
# implemented do_suspend. We could just check to see of do_suspend was
# already declared using command_exists, but using a dedicated environment
# variable makes it easier to debug when we have to know what sleep module
# ended up claiming ownership of a given sleep method.
if [ -z "$SUSPEND_MODULE" ] && command_exists s2ram &&
( grep -q mem /sys/power/state ||
( [ -c /dev/pmu ] && check_suspend_pmu; ); ); then
SUSPEND_MODULE="uswsusp"
do_suspend()
fi
if [ -z "$HIBERNATE_MODULE" ] &&
[ -f /sys/power/disk ] &&
grep -q disk /sys/power/state &&
[ -c /dev/snapshot ] &&
command_exists s2disk; then
HIBERNATE_MODULE="uswsusp"
do_hibernate()
s2disk
fi
if [ -z "$SUSPEND_HYBRID_MODULE" ] &&
grep -q mem /sys/power/state &&
command_exists s2both &&
check_hibernate; then
SUSPEND_HYBRID_MODULE="uswsusp"
do_suspend_hybrid()
uswsusp_get_quirks
s2both --force $OPTS
if [ "$METHOD" = "suspend_hybrid" ]; then
add_before_hooks uswsusp_hooks
add_module_help uswsusp_help
fi
fi
edited Oct 31 '13 at 23:51
answered Oct 31 '13 at 13:04
markmark
192
192
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f12383%2fhow-to-go-automatically-from-suspend-into-hibernate%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In my research I found the same Linux Mint thread, but "Suspend Sedation" is not an official Microsoft term for that feature and as far as I can tell was invented by the Linux Mint forum user who mentioned it.
– ayan4m1
Nov 10 '10 at 20:43
Is there a better name for that feature?
– Sergey Stadnik
Nov 11 '10 at 2:01
As far as I can tell, there is no universally accepted name for the feature. "Hybrid suspend" is used by some, "suspend sedation" is used by that one Linux Mint forum user, and I've heard "hibernate and suspend" used to refer to the process before. Microsoft officially refers to it as "hybrid sleep," for Windows 7 at least.
– ayan4m1
Nov 11 '10 at 13:16
2
@ayan4m1 I realise this is an old question, but I think it is important to clarify this. Hyrbid sleep is not the same as "Sleep then hibernate after a specified time". Hybrid sleep simply becomes hibernate when power is lost, through battery running out. The behaviour described by the OP does not require Hybrid Sleep to be enabled.
– Paul
Dec 9 '11 at 2:51