How can I make the laptop wake up from suspend once in a while to check battery level, and if it’s low; hibernate?
Last night I suspended the laptop with AC connected, then we got a power outage. When I woke up the battery was 0%, and all my work in progress was gone. If the suspend mode included some kind of battery level monitor, the system would have hibernated rather than running out of battery. (This has been default behavior in windows since 98.)
Which I knew how to save… All documents were saved, but I had 30ish pdf’s open, scrolled down to whatever section I needed to look at, 20ish documents documents/spreadsheets/presentations open with various information selected to show how far I’d got in gathering the information, 9 instances of firefox open, logged in as different users, in progress of filling in various forms based on the information gathered from the documents mentioned above.
And at this subject windows works out of the box, while I have not been able to make manjaro work, even though I’ve tried several times over the 5 years I’ve been using it.
Clearly it’s not. I suspect the functionality might be a part of the suspend-then-hibernate function. But I can’t figure out how the set that up. It’s not an option in xfce power manager. The guides I find are either old or for other distros. Some suggest enable suspend-then-hibernate.service, but no such service exists. (I do find suspend-then-hibernate.target.) Some suggest creating symlink in /etc to suspend-then-hibernate.service manually, but I can’t find the service file in my system.
Is there a guide available the applies for manjaro?
(I do have hibernate working. If laptop runs out of battery when powered on (not suspended) it goes to hibernate, and resumes without any issue.)
And if I get the suspend-then-hibernate to work, does it do what I’m looking for? I’m not looking for a timer to hibernate after X minutes of suspend. I’m only looking for hibernate if battery goes low.
hybrid sleep is defined as save state to memory and disk
if not simultanously, at least in the same process
suspend then hibernate is a little different
it suspends to ram
if not restored within a short period
it writes to disk
power off
But you also need to consider the threshold for Critical battery - as it was the lack of battery power that caused your system to fully shutdown without saving state.
I don’t have “hybrid sleep” as an option in power manager. My options are “do nothing”, “suspend”, “hibernate”, “shutdown”, “ask”.
As I said, hibernate works perfectly while laptop is powered on (not suspended). I have working suspend, working hibernate, working shutdown. But not working suspend-then-hibernate.
I cannot locate any suspend-then-hibernate.service file anywhere on my system. I do find suspend-then-hibernate.target
If suspend-then-hibernate does not run battery level check, it shouldn’t be impossible to modify it’s behavior a bit. If all suspend-then-hibernate does is to set a wake up timer to run a script that hibernates, there must be something to that script that, before it hibernates performs a check if the wake-up was caused by timer or user interaction. It shouldn’t be impossible to modify that script to do something like this:
if wakeup caused by user interaction
then resume to normal operation
else
if battery level < threshold && AC not connected
then hibernate
else suspend-then-hibernate
fi
fi
Basically this logic should wake up the pc and check if battery level still is above a threshold. If there is still plenty of battery, reset the suspend-then-hibernate timer and put the laptop back into suspend. If it’s not plenty of battery, run hibernate. If the hibernate delay timer is set shorter than the time expected for battery to drain from threshold to 0% during suspend, this should ensure battery not running out while suspended.
$ cat /usr/lib/systemd/system/systemd-suspend-then-hibernate.service
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=System Suspend then Hibernate
Documentation=man:systemd-suspend-then-hibernate.service(8)
DefaultDependencies=no
Requires=sleep.target
After=sleep.target
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/systemd-sleep suspend-then-hibernate
I think swap is not the issue, as hibernate works.
The reason I use suspend rather than hibernate when the charger is connected is to reduce write operations to the ssd. Hybrid sleep does not seem useful to my needs.
Hybrid sleep is available is available in the shutdown / restart / suspend /… dialog box. But it is not available in the power manager settings dialog.
Suspend-then-hibernate is not available any other place than command line.
Then you’ll have to allow hibernate-then-suspend in systemd. Edit '/etc/systemd/sleep.conf` and uncomment the necessary values:
sudo nano /etc/systemd/sleep.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file (or a copy of it placed in
# /etc/ if the original file is shipped in /usr/), or by creating "drop-ins" in
# the /etc/systemd/sleep.conf.d/ directory. The latter is generally
# recommended. Defaults can be restored by simply deleting the main
# configuration file and all drop-ins located in /etc/.
#
# Use 'systemd-analyze cat-config systemd/sleep.conf' to display the full config.
#
# See systemd-sleep.conf(5) for details.
[Sleep]
#AllowSuspend=yes
#AllowHibernation=yes
AllowSuspendThenHibernate=yes
#AllowHybridSleep=yes
#SuspendState=mem standby freeze
#HibernateMode=platform shutdown
#MemorySleepMode=
#HibernateDelaySec=
#SuspendEstimationSec=60min
Once it’s possible to enter & wake from the hibernation state, we can then transition to the suspend-then-hibernate step. For this, we have to configure systemd to handle entering hibernation from the suspend state after a given period.
(D1) Set a delay in the sleep.conf, which will inform systemd how long to stay in the suspend state before hibernating.
(a) Add the following to the “/etc/systemd/sleep.conf” file.
[Sleep]
AllowSuspendThenHibernate=yes
HibernateState=disk
#Suspend then Hibernate after 15 min
HibernateDelaySec=15min
(D2) Inform systemd the behavior of the power switch, lid closing, and suspend state.
(a) Add the following to “/etc/systemd/logind.conf”