languitar / autosuspend

A daemon to automatically suspend and wake up a system
https://autosuspend.readthedocs.io
GNU General Public License v2.0
74 stars 14 forks source link

Add systemd ScheduledShutdown (and reboot) wakeup check #253

Open Flowdalic opened 1 year ago

Flowdalic commented 1 year ago

Please consider adding a new wakeup check for systemd scheduled reboots. In case there is a scheduled shutdown or reboot pending, the wakeup check should schedule a wakeup briefly, say 3-5 minutes, before the scheduled shutdown or reboot. One can retrieve information about scheduled shutdowns or reboots via e.g.,

$ busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown
languitar commented 1 year ago

Interesting, I didn't even know such a feature exists. What is your use case for this?

Flowdalic commented 1 year ago

We schedule automatic reboots based reboot-required.pkgs and needrestart information. If the systems suspend while a scheduled reboot is pending, and awakes after the time of the scheduled reboot, the scheduled reboot fails. Waking the system up, briefly before the scheduled reboot, causes the system to reboot.

languitar commented 1 year ago

I just wanted to look at this issue again. What command do you actually use to schedule a reboot?

languitar commented 1 year ago

Btw, looking at this StackExchnage question, it seems that for the moment you should be able to create a simple script for the Comannd wakeup check to workaround the lack of the feature right now.

Flowdalic commented 1 year ago

What command do you actually use to schedule a reboot? For example

$ shutdown --reboot +02:00 "Scheduled reboot for maintenance"
Flowdalic commented 1 year ago

Btw, looking at this StackExchnage question, it seems that for the moment you should be able to create a simple script for the Comannd wakeup check to workaround the lack of the feature right now.

Yes we have something like that in place

#!/usr/bin/env bash
set -eou pipefail

# Although this file is called wakeup-before-scheduled-reboot we
# actually check for the next scheduled reboot *or* shutdown. But this
# does not really matter, as we want to wakeup in both cases and we
# only schedule reboots.
SCHEDULED_SHUTDOWN_INFO=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)

declare -i SCHEDULED_SHUTDOWN_SECS
SCHEDULED_SHUTDOWN_SECS=$(echo "${SCHEDULED_SHUTDOWN_INFO}" | cut -d ' ' -f 3)

if [[ ${SCHEDULED_SHUTDOWN_SECS} -eq 0 ]]; then
    # Output the empty string to make autosuspend happy.
    echo ""
    exit
fi

BEFORE_SCHEDULED_SHUTDOWN_SECS=$(( SCHEDULED_SHUTDOWN_SECS - 180 ))

echo "${BEFORE_SCHEDULED_SHUTDOWN_SECS}"

which is added as script to autosuspend's configuration

[wakeup.BeforeScheduledReboot]
enabled = true
class = Command
command = /etc/autosuspend/wakeup-before-scheduled-reboot
languitar commented 1 year ago

Fun thing about the shutdown command: that is provided by the systemd compatibility layer for sysv. However, I couldn't find any way to achieve the same behavior without this compatibility command.

Flowdalic commented 1 year ago

Fun thing about the shutdown command: that is provided by the systemd compatibility layer for sysv. However, I couldn't find any way to achieve the same behavior without this compatibility command.

Probably because the functionality already exists in that said program. I assume it is meant as drop-in replacement, not as something that will ever disappear. That said, I wouldn't be surprised if this where possible using some dbus API.