You are not logged in.

#1 2022-06-16 00:25:13

Wild Penguin
Member
Registered: 2015-03-19
Posts: 319

Check conditions before automatic suspend/hibernate [SOLVED/ideas]

Hi,

Let's suppose a following situation for a desktop computer (i.e. non-laptop) which is also working as a home server:

  • The user(s) want to save power by enabling automatic suspend/hibernate

  • But it is not desirable that some processes get interrupted by suspend/hibernate, they (processes) need to be able to override it

  • And sometimes it might be desirable that even a manual suspend/hibernate request given by the user gets cancelled and a message is printed instead (something like: "suspend cancelled because ... is in progress")!

I.e. we have a multi-process(or user) computer, where sleep occurs only if all processes (or parties, users) agree!

Now I have a hunch what I would to do manage this, but I also have a hunch I'm not the only one with this kind of problem. Are there any ready-made solutions to manage this? I've tried a few search words but could not find any (perhaps wrong search terms...).

This would be easier if it was only processes (I could have an "umbrella" process/cron job etc. checking everything has stopped, and even set up wakeup automatically via ACPI/BIOS) - but then there might be the desktop user. FWIW I'm currently using KDE Plasma. KDE Plasma already has an option to run a custom script after a delay, which could be used to signal an "idle condition" to an external process - and manual signal from the user "it is OK to suspend the computer" could be facilitated by locking the session (I know, it's a bit dirty, but should work).

Any thoughts / experiences on solutions / ideas are welcome!

Last edited by Wild Penguin (2022-06-16 19:07:12)

Offline

#2 2022-06-16 01:41:43

mpan
Member
Registered: 2012-08-01
Posts: 1,188
Website

Re: Check conditions before automatic suspend/hibernate [SOLVED/ideas]

In a typical multi-user environment the concept of inactivity becomes hard to define.

For a single user running a graphical DE the inactivity is bound to the seat. No one is sitting before the screen, so go to sleep. A component in DE detects no input events for a given amount of time⁽¹⁾ and tells the system to suspend itself. No similar mechanism for your scenario. Even if all users would run a xorg session, which I suspect⁽²⁾ might register inactivity, the entire solution is intended for local use: the users will not get a notification, a method to wake up the server, a lock screen and so on. They will simply see their connection getting dropped and no way to reconnect. Which brings us to one more important question: if you find a way to suspend the system, how will you wake it up remotely?

Tangential to the problems mentioned above is preventing a suspend from happening. That can be done acquiring an inhibitor lock. May be done by running a process through systemd-inihibit. As long as the process is running, the selected operation request (e.g. suspend) will be rejected by systemd. Note that an inhibitor lock must be acquired before a suspend attempt: it’s prevention, not a response. To complete the picture: there are also suspend hooks. While not directly responsible for any of the concerns discussed, you are likely to need them for various kinds of cleanup, notification and logging.
____
⁽¹⁾ For example X11 supports it as the screensaver extension and XScreenSaverQueryInfo provides idle time (example in xprintidle).
⁽²⁾ Never tried that with remote graphical sessions.


Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

#3 2022-06-16 19:06:35

Wild Penguin
Member
Registered: 2015-03-19
Posts: 319

Re: Check conditions before automatic suspend/hibernate [SOLVED/ideas]

Hi mpan, thanks for your input!

You have good points in your post. I believe with the help of it I can find a setup which will work for me! Especially systemd-inhibit was something I was looking for and will take care of most of the situations I need. Suspend hooks might become useful, too (for choosing a right wake up time, for example, if many processes need one). Also, I don't actually have remote users. Perhaps I was talking in a bit too vague terms, trying to cater to all who might wander to this thread, and forgetting my more specific use case(s).

To open up the issue a bit, I think it can be divided into two sub-problems: when to hibernate/sleep(/shutdown?) and when to wake up.

First, for hibernating:

I believe systemd-inhibit is exactly what I need. The DE can try to hibernate after inactivity (of the single desktop user session or manually by the user), but will be blocked if needed (I just tested this and it seems to work). Furthermore: any process I use can be tweaked so that the set the lock and try to hibernate after finishing and removing their lock.

But this might not work for all other use cases. A suggestion: If there really was a multi-user setup, I'd think something like a per-session "idle" or "away" -flag (for example, locking screen, running a script etc.) - and some umbrella daemon. Or just some clever use of inhibit locks (only lift them if the user controlling one wants to / is "idle", which is down to the user to automate). In this kind of use case no user actively starts a hibernation, or at least is in no absolute control over it (as there are many users). But this is just speculation for a more complex use case, I don't need it, at least currently. It definitely can get a bit hairy and may need tweaking for the specific use case/setup. But the pieces are there!

Waking up:

I wasn't asking for this since in my use case it is quite simple: most of the time wake ups will be done locally by me. If they are not, the time needed for next wake up is known beforehand - which is the use case for ACPI/BIOS wakeup.

But this might not always be that simple for some other use cases. Sometimes perhaps WOL could be used - but that obviously requires some other computer/device in the network to send the packets, and someone/something needs to trigger this ... which kind of feel like going in circles (we need a computer running to wake up a computer...). Some routers (OpenWRT) can be set up so that they relay WOL packets - but I wouldn't do that unless a VPN or similar is in place. YMMV!

Marking as [SOLVED], however I will return here in case I have something substantial to post. For now, I believe all I need are a few inhibit locks and ACPI wakeup.

Last edited by Wild Penguin (2022-06-16 19:09:35)

Offline

Board footer

Powered by FluxBB