Differences between revisions 26 and 27
Revision 26 as of 2021-07-03 19:21:09
Size: 4172
Editor: ?azerttyu
Comment: typo
Revision 27 as of 2023-08-30 06:50:53
Size: 4290
Editor: TobiasFrost
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Note: This might be outdated, systemd supports "suspend-then-hibernate" at least since bookworm. See systemctl(1).

Note: This might be outdated, systemd supports "suspend-then-hibernate" at least since bookworm. See systemctl(1).

This page explains how to have a laptop first Suspend to RAM then after a some time save state to disk and power off completely. Inhibiting a system from suspending/hibernating is explained at Suspend.

Suspend on lid close

In order to trigger suspend on closing the lid, make sure that there is a line in /etc/systemd/logind.conf that reads HandleLidSwitch=suspend, and that it is not commented out (prefixed with #).

The default values are :

  • HandleLidSwitch=suspend

  • HandleLidSwitchDocked=ignore

Example logind.conf:

# /etc/systemd/logind.conf
[Login]
HandleLidSwitch=suspend
HandleLidSwitchDocked=suspend
# All default lines with comments removed for clarity.

After making the change to logind.conf, run systemctl restart systemd-logind.service. After doing so closing the laptop's lid should cause the laptop to suspend..

If you are running a simple xsession, and want to make sure your screen locks on suspend, you can install the xscreensaver and xss-lock packages, and add the lines:

xscreensaver &
xss-lock -- xscreensaver-command --lock &

to your .xsession-file

From Suspend to Hibernate

When the laptop suspends to ram, it uses very little power, but the current session is not persisted to disk, so if the battery runs out, the system will be forced to do a full boot on resume. There are two ways around this: the classical way is hybrid-sleep: when suspending the machine also writes current state to disk, as with hibernate — so if power runs out no data is lost, and the session can be resumed.

Unfortunately hibernate, even with an solid-state drive, takes longer than entering suspend — and this can be inconvenient at times.

We can use the real-time wake timer to allow the system to wake up from sleep and go straight back to hibernation — if the system has been suspended for a given amount of time. With systemd, we can do this via a service unit; create the file /etc/systemd/system/suspend-sedation.service consisting of the following:

# /etc/systemd/system/suspend-sedation.service
[Unit]
Description=Hibernate after suspend
Documentation=https://bbs.archlinux.org/viewtopic.php?pid=1420279#p1420279
Documentation=https://bbs.archlinux.org/viewtopic.php?pid=1574125#p1574125
Documentation=https://wiki.archlinux.org/index.php/Power_management
Documentation=http://forums.debian.net/viewtopic.php?f=5&t=129088
Documentation=https://wiki.debian.org/SystemdSuspendSedation
Conflicts=hibernate.target hybrid-sleep.target
Before=sleep.target
StopWhenUnneeded=true

[Service]
Type=oneshot
RemainAfterExit=yes
Environment="ALARM_SEC=300"
Environment="WAKEALARM=/sys/class/rtc/rtc0/wakealarm"

ExecStart=/usr/sbin/rtcwake --seconds $ALARM_SEC --auto --mode no
ExecStop=/bin/sh -c '\
ALARM=$(cat $WAKEALARM); \
NOW=$(date +%%s); \
if [ -z "$ALARM" ] || [ "$NOW" -ge "$ALARM" ]; then \
  echo "suspend-sedation: Woke up - no alarm set. Hibernating..."; \
  systemctl hibernate; \
else \
  echo "suspend-sedation: Woke up before alarm - normal wakeup."; \
  /usr/sbin/rtcwake --auto --mode disable; \
fi \
'

[Install]
WantedBy=sleep.target

After creating this file, enable it: sudo systemctl enable suspend-sedation. Now you should be able to enter suspend-mode, either by closing the lid, or via systemctl suspend, and have the system suspend immediately, and be ready to wake up quickly for 5 minutes (300 seconds). After that time, the system should briefly wake up on its own, and immediately hibernate.

In order to see the messages logged by this unit, either use journalctl -u suspend-sedation, or look in /var/log/daemon.log for lines containing "suspend-sedation".

This solution is inspired by: https://bbs.archlinux.org/viewtopic.php?pid=1256340, but uses the rtcwake command from the util-linux package (an essential package, it should be installed on all Debian systems by default). The --auto flag adjusts for the situation where the real-time clock might *not* be set to UTC — this is commonly the case on systems that dual-boot Microsoft Windows, for example.