Suspend then power down

I am looking for a way to suspend the laptop, not go to hibernate but power down once a certain amount of time has gone. This avoids swap partitions and secure boot problems, but still saves the battery when not in use.

I found the following possible solution, but since I am not too familiar with Linux I am wondering whether this would work: suspend-then-shutdown sleep option? / Newbie Corner / Arch Linux Forums

$ cat /etc/systemd/system/systemd-suspend.service
[Unit]
Description=Suspend; Shutdown if not used for a period of time

[Service]
Type=oneshot
ExecStart=/usr/local/bin/suspend-then-shutdown

$ cat /usr/local/bin/suspend-then-shutdown
#!/bin/sh
delay=$((2*3600))
start=$(date +%s)
rtcwake -s $delay -m mem
end=$(date +%s)
if [ $((end-start)) -ge $delay ]; then
        systemd-run --on-active=5 systemctl poweroff
fi

Please note that I've used this on Debian 11, currently don't have Arch to test but I suppose that it should work with newer systemd as well.

Note that I've replaced (not overridden) the suspend target, not suspend-than-hibernate, which may seem more appropriate. But invoking systemctl suspend-than-hibernate is refused for me, because I use Secure Boot and lockdown mode doesn't allow hibernation.

UPDATE 2023-06-17: replaced shutdown -h now by systemd-run [...] poweroff - it seems like newer systemd doesn't allow entering shutdown when in midst of sleep target.

Last edited by MartinPulec (2023-06-17 16:46:46)

I found this script but also can't assess it. You would have to adapt the time you want until shutdown.

Quite a bit more elaborate it seems. What I would love to figure out before trying this type of thing is how to remove the code if it does not work as required... It feels uncomfortable spreading and leaving spurious code snippets across the system. Maybe a newbie thing...?

1 Like

Oh yes, that's always really important to me too! As a newcomer, you often can't even estimate what will be configured. Let's wait until an expert gets in touch. I'm interested in that too.

This is a wise thing.

The above script will not work on Zorin OS because Zorin OS uses a later version of SystemD in which this was patched to prevent these kinds of workarounds.
For one, they create security exploits and for two, they can break the system.

When a computer is in sleep or suspend, the CPU is powered down. This necessitates that user space processes are stopped entirely.

In order to execute a shutdown command, you would need to wake the system from suspend first. This means that your above Fedora Script, which is already risky and brittle, would need to be far more elaborate; containing a wake command and another new script placed in another location to run a shutdown sequence.
The wake process is in /sys/class/rtc/rtc0/wakealarm so the user would need to place a script in /lib/systemd/system-sleep/wakealarm that wakes the system after a certain interval.

Then the user must place a script in /usr/local/bin/ to check the time passed and shutdown the machine if the passage of time meets the threshold...

Then another addition in /etc/systemd/system/ to create a systemd service to execute the shutdown, which is called upon by the above script.

I would never do this.

Primarily due to lack of need weighed against the involved and risky scripting of the essential systemd components.
Suspend reduces the machine to a minimal power consumption state. Very Minimal. We cover above that the CPU is powered down and the kernel has detached from user space.
Unless you are leaving the machine for extensively long periods of time (There is zero need to shutdown the machine after two hours of suspend), there is no conceivable need to have the computer self wake after a week, check the time, then tell itself to go ahead and power off.

2 Likes

Very useful thank you.
The reason I was looking into this was that when I left the laptop in suspend on my desk, during the night, the power supply shuts off automatically... When I wanted to start work in the morning, after about 10 hours of suspend, there was no battery left and I could not wake up the laptop without charging it a bit.
So very surprised to hear a modern laptop should last days in suspend... What is going wrong then?

You might try calibrating your battery, though after what you describe, it may seem a bit absurd.

Calibrating is fully charging the battery once, then letting it run until the battery dies (Not including suspended time). You want to do a minimum of two cycles of this.

A battery is not a container that gets filled with electric. It is a chemical state, not an electrical one.
Charge is about electron flow, not electrons filling or emptying.

Because of this, each battery is going to show some variation in how flow transfers that you can monitor, and the rate at which it flows over time. Calibrating it is done by getting a good sample of these curves.
A properly calibrated battery can seem to keep a charge longer because the system demand and usage curves can be matched to the actual flow curve of the battery.

During use - this is not very applicable as the user will override any trends by launching apps, but during suspend or sleep - this matters.

Power consumption is very low, usually less than 1W to ~2W.
Assuming an average 50Wh battery, a high end notebook computer on suspend should last about 75 to 150 hours.
A less expensive mid-range notebook computer can range 35 to 50 hours.

Even a low grade and poorly optimized machine should last better than 15 hours.

What may affect this includes a battery that is degraded. It works, but not fully.
USB devices that intermittently wake the machine, or network devices that wake and check network activity before resuming suspend can cause spikes in power drain, too.

Nvidia drivers are known to be disobedient. This can cause the GPU to power on even when the computer is suspended.
There are some wifi cards that won't suspend or won't fully suspend.

There are some terminal commands that can monitor what is using power during suspend - but I do not have them in my notes. I will need to look them up.

In the meantime, you might change the sleep mode to deep, since some configurations use a "light sleep" mode as default. A "deep sleep" mode will be more true to a 'suspend only to RAM function'.

echo deep | sudo tee /sys/power/mem_sleep

EDIT:

I believe that TLP does this automatically:

sudo powertop --auto-tune

Once you set the above, in addition, run:

sudo systemctl suspend

After letting the machine run on suspend for about 10 hours, wake the machine and run the following to check logs:

sudo journalctl | grep -i suspend

Another log

sudo cat /sys/kernel/debug/wakeup_sources

1 Like

Before the tests suggested... I noticed I had USB charging enabled in bios.
The laptop connects to a usb-c hub overnight and could have been powering that and the attached devices while in suspend... I disabled this... Let's see

1 Like