[How-To] Setting-up suspend-then-hibernate in XFCE

Difficulty: ★☆☆☆☆

Introduction - how i came to this

I am a fan of daily proper shutdowns, especially since my laptop boots in about 20-30 seconds. Still, in some situations it is convenient to just clap the lid and put the pc to sleep, for example to move the laptop to another room or building. And in rare cases, one can forget it in that sleepy state for a long time. It recently happened to me. I put it to sleep that way, something else happened and i didn’t come working on the laptop till the end of the day. The next day i saw the laptop didn’t want to start on battery - systemd informed me on boot time the battery is extremely low and will immediately shut down. The battery has drained from 60% to 0 over the night. I found this a bit stupid. Yes, i knew there is an emergency action to hibernate so no data loss occurs, but still - the machine has worked for hours for no reason. I thought i had forgot to set it up properly.

So i went to XFCE power settings. I always say the simple plain XFCE has 90% of the features of the more modern and sophisticated desktop environments. Well, this one was in the other 10% :sweat_smile: There was no option in the GUI to sleep for some time and then to automatically hibernate to preserve battery. It would only hibernate automatically at some critical battery level.

After some research i discovered that although XFCE does not have GUI settings for suspend-then-hibernate, systemd itself is perfectly capable of doing it. It just had to be configured, and the trick was to disable XFCE from overriding systemd, which is the default behaviour.

IMPORTANT NOTE

This assumes your suspend and hibernate are configured properly and work properly on their own (swap, kernel parameter, suspend state, etc.). This article takes for granted the prerequisites are fulfilled - if not, check the Arch wiki how to set up hibernate.
I write this only because there is no GUI for it, which is a problem for novice users.

So, without further ado, here is how to do it:

  1. configure systemd with drop-in files, to prevent unnecessary pacnews on next systemd update. We will make changes to sleep.conf and logind.conf, so we need to create the directories and files.
sudo mkdir /etc/systemd/sleep.conf.d
sudo touch /etc/systemd/sleep.conf.d/suspend_hibernate.conf
sudo micro /etc/systemd/sleep.conf.d/suspend_hibernate.conf

Note that i use micro as my editor of choice, you can of course use nano. Paste the content (change the timeout to switch from suspend to hibernate to taste)

[Sleep]
AllowSuspendThenHibernate=yes
HibernateDelaySec=30min

Save with ctrl+s, exit with ctrl+q. Proceed with the second file.

sudo mkdir /etc/systemd/logind.conf.d
sudo touch /etc/systemd/logind.conf.d/lid.conf
sudo micro /etc/systemd/logind.conf.d/lid.conf

the content of the file is

[Login]
HandleLidSwitch=suspend-then-hibernate
HandleLidSwitchExternalPower=suspend-then-hibernate

You now have to either restart the pc or just the logind with

sudo systemctl restart systemd-logind.service
  1. The second step is to disable the xfce overriding
xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/logind-handle-lid-switch -n -t bool -s true

Now, whenever you clap the lid of the laptop, it will sleep for 30 minutes and then wake up and hibernate automatically (instead of draining the whole battery first). Enjoy.

1 Like