Delayed hibernation on Linux with SystemD

One thing that I liked on modern laptops with MS Windows or Apple macos was this power saving technique od delayed hibernation. On set timeout of inactivity or after closing laptop’s lid it goes into suspend and then after sometime into hibernation. That’s great because it stops battery drainage needed to support RAM during regular suspend. With it I can go for days of work without the need to recharge my laptop. From many methods to achieve this with SystemD this one worked always and keeps working for me to this day.

But first things first. You need to check if your machine is capable of hibernation. Generally you need to have a swap partition at least a little bigger then your RAM capacity. My machine is also encrypted with LUKS so my kernel line has the following line included:

  resume=/dev/mapper/vg0-swap

While adding it to your bootloader config make sure that you account for your distro specific variables like different name of swap partition in LVM config (you can check it by listing /dev/mapper/). Additionally my mounting of swap partition in /etc/fstab goes like this:

UUID=r5f026te-e9d2-4973-81fd-998e04dae75f    none    swap    defaults    0 0

After your swap is properly setup you can go ahead and check if hibernation works by executing:

sudo systemctl hibernate

If your laptop went dark after a minute or so and resumed after you pushed the power button you’re good to go. Now it’s time for the actual delayed hibernation setup.

Backups, backups, backups. To stay on the safe side if you have file /etc/systemd/system/systemd-suspend.service present, make a copy:

sudo cp /etc/systemd/system/systemd-suspend.service /etc/systemd/system/systemd-suspend.service.orgi

Then we do the actual change to replace original suspend action with our desired one:

sudo ln -s /usr/lib/systemd/system/systemd-suspend-then-hibernate.service /etc/systemd/system/systemd-suspend.service

And that’s mostly it. Now we can adjust the timeout after which our machine will go from suspend to hibernation. This one takes some experimentation. For me optimal value is 15 minutes. If I didn’t open my laptop after that time I’m probably out commuting to another location or done fo the day. To make the adjustment we edit /etc/systemd/sleep.conf, uncomment the following line and set the time:

HibernateDelaySec=15min

Now any suspend action called by you or any kind of automation will trigger delayed hibernation.

Please note that in different distros documentation you can find many other tutorials and ways to set this up. Most of them is outdated. The one presented above while being the oldest one, is working for me at any point of time and in any GNU/Linux distribution with SystemD.

When you’ll find that you lack some of the files or some other differences please follow this thread on mastodon and feel free to contact me.

For example on Ubuntu you might find service file that you need to symlink under:

/lib/systemd/system/systemd-suspend-then-hibernate.service
Continue Reading