How to Set systemd to Hibernate Instead of Suspend on Laptop Lid Close
By Andrew Powell, published 04/10/2020 in Tutorials
In this article we'll be seeing how to change the setting that sets how systemd (the most common init system on GNU/Linux systems) handles lid closes. By default, when you close the lid on your laptop it will in most cases suspend (sleep). What if you wish to for the system to hibernate instead? It's quite easy to change.
User/Difficulty Level: Intermediate
Now it's worth bearing in mind this article involves changing/adding a couple of lines to a configuration file and is aimed at systems running a more bare bones setup, such as a window manager instead of a full fledged desktop environment such as GNOME or KDE. In these environments, changing this setting is likely as easy as just selecting the option in a drop down box in the System Settings (under Power Management).
However, if you're like me and you are running a more custom configuration without any desktop environment taking control of these settings, and your system is using systemd, you'll need to modify a systemd configuration file.
First of all, you may ask why anyone would want to change this setting when suspend/sleep generally works fine and is the faster method to resume the computer. Well, unfortunately, suspend doesn't always work perfectly on all systems. My old Sony Vaio VPCEE26FG laptop is one such system, and in fact, Linux is not even the culprit here - this same machine won't resume from suspend even in a Windows operating system! I suspect there's a hardware issue here. Whatever the case, resuming from suspend isn't an option. It goes to "sleep" just fine, but often never wakes up - I'm greeted with a never ending black screen and no amount of workarounds has ever reliably fixed it. Curiously, hibernation does work completely fine and hence is the only reliable state saving mechanism for this machine.
Other reasons for preferring automatic hibernation may also be that you may have unreliable power at your premises. With suspend, usually the system saves to "RAM" and if power is lost, so is the state of the system, as it requires constant power. Not so much of a problem with a battery, such as is the case with laptops, but then again some older laptop's batteries are even no good anymore.
With hibernation, the saving of the system state is stored to the hard disk, which doesn't require a constant power supply. Resuming from this method is always slower, but generally still is always faster than a fresh boot and of course all your work such as opened windows, running applications etc is preserved.
Okay enough of the why, onto the how.
Important Note: This assumes your system is already capable of actually hibernating. On a bare bones, mostly DIY GNU/Linux distribution such as Arch Linux the system may not be setup out of the box to hibernate. If in doubt, simply try hibernating the system - if it boots up to a fresh system when you try powering it on, you know it didn't hibernate correctly. This is beyond the scope of this article, but if you would like me to post a guide on this as well please let me know in the comments. Otherwise, please consult this very good Arch Linux wiki article here for instructions.
Making systemd prefer hibernation
This is really very simple, all we need to do is change/add a couple of lines to the file /etc/systemd/logind.conf
.
Open this file up in your favourite editor of choice and of course you will need super user permissions to edit this file. There will be a bunch of default settings already in this file, commented out with a '#'. We could uncomment and change a few of these, but perhaps it's better to leave them as the original defaults, commented out as they are, and add the uncommented new values.
Input these lines at the bottom of the file:
HandleSuspendKey=hibernate
HandleLidSwitch=hibernate
HandleLidSwitchExternalPower=hibernate
Save and exit.
That's all there is to it, all you'll have to do now is reboot the machine for it to take effect. From now on, systemd will automatically choose to hibernate instead of suspend when the laptop lid is closed.
Keep in mind, if you're not familiar with resuming from hibernation, it works a bit differently to suspend. Your machine will actually power off fully and you will have to press the power button to activate it again. At first it even will appear that the system is booting from scratch (BIOS screen etc will appear, instead of waking almost instantly like in the case of suspending). But, so long as your system hibernation is configured properly, you will find that once the system starts booting to the operating system, instead of a lengthier wait and having to log in (etc), you will find your saved system appear on your screen as it was before you entered hibernation.
And that's it. Until next time!