You are not logged in.

#1 2024-09-27 22:42:51

phonky
Member
Registered: 2008-12-19
Posts: 78

Autosuspend a server after some inactivity time?

All I have found searching about suspend is related to laptops and desktop environments. Closing the lid, inactivity through a daemon of the DE and stuff like that.

But I have been looking to auto-suspend a server. No desktop env (is this what's usually called headless?). Only remote login via ssh.
Is it possible that after some inactivity time (which in my case mostly means no network requests of any type, be it http or ssh), such a server can be put to suspend?

Offline

#2 2024-09-27 23:23:52

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,173

Re: Autosuspend a server after some inactivity time?

I would start here: https://wiki.archlinux.org/title/Wake-on-LAN
Then there is the problem of when to put the system to sleep.  I'd start with the harder problem of waking up by working through the linked article.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2024-09-28 07:34:20

seth
Member
Registered: 2012-09-03
Posts: 58,243

Re: Autosuspend a server after some inactivity time?

some inactivity time (which in my case mostly means no network requests of any type

This might not actually work the way you want in case the system generates unprovoked traffic, but you could poll /sys/class/net/*/statistics/tx_bytes once a minute and if the value didn't change on the relevant NIC for 10 minutes or so, send the system to sleep.

You might have to monitor the port eg. w/ tcpdump

tcpdump -q --count -c1 -i eth0 -Q in port 443 or port 80 or port 22

should fire anytime you get inbound traffic on those ports.
You could fork that, sleep some time (5 minutes or so) and if the job is still active, kill it and suspend the system.
If it's not, start a new round. This would sleep the system after 10 minutes - ε of idle network.

If you're actually interested in waking the system remotely, the challenge will be to do that "filtered", you either get it to wake (only) on the WOL packet (which you'll have to explicitly trigger) any physical activitiy (what will cause spurious wakeups for almost sure) or not at all. This very much depend on the specific HW.

Offline

#4 2024-09-28 16:02:32

phonky
Member
Registered: 2008-12-19
Posts: 78

Re: Autosuspend a server after some inactivity time?

Thanks or the helpful replies!

I have also been thinking that maybe I could just run a DE "in the background". I run hyprland with hypride on my laptop. It occurred to me I could just install and run the same on the server. `hypridle` can put the machine to suspend after some inactivity. While the WoL functionality could wake it up via network on request.

It might work, but I'll need to try.

Offline

#5 2024-09-28 16:17:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Autosuspend a server after some inactivity time?

I highly doubt that'd work at all.  First, installing a DE just for an idle timer is a bad idea to say the least.  But worse, none that I'm aware of account for network activity, only local mouse and keyboard input - but there never will be local input - so it will go into sleep mode as soon as the timeout expires.  This could be replicated just with a two line script rather than a full DE.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2024-09-28 17:19:41

phonky
Member
Registered: 2008-12-19
Posts: 78

Re: Autosuspend a server after some inactivity time?

> ...so it will go into sleep mode as soon as the timeout expires

Correct, but then I was thinking that WoL would be able to wake it up from that sleep, and if there is no more activity, the DE would put it to sleep again...
The big question here of course is, could the DE put it to sleep in the middle of some network transfer?

But indeed something on the network level would be cleaner.

Last edited by phonky (2024-09-28 17:24:53)

Offline

#7 2024-09-28 17:21:41

cryptearth
Member
Registered: 2024-02-03
Posts: 908

Re: Autosuspend a server after some inactivity time?

as noone asked yet: what is the idea behind this? servers are there to serve services - so to be ready at all times
putting a server into sleep or even deeper somewhat defeats the idea of a server - to when using this word not to name a computer but a person (a waiter): thier job is to actively wait for your call (I'm unsure if "waiter" originates from this "purpose") - putting a waiter into sleep (let them sit down and doze off) just so first have to wake them up somewhat defeats thier job to actively pay attention for your call

so - from our favorite "that's an XY problem": what's your idea behind this? maybe there's a better solution to your problem

Offline

#8 2024-09-28 18:17:30

Lone_Wolf
Administrator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 12,891

Re: Autosuspend a server after some inactivity time?

@phonky :
your reply was reported to moderators instead of submitted to post, you'll have to repost it.


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.

clean chroot building not flexible enough ?
Try clean chroot manager by graysky

Offline

#9 2024-09-28 19:41:58

seth
Member
Registered: 2012-09-03
Posts: 58,243

Re: Autosuspend a server after some inactivity time?

Correct, but then I was thinking that WoL would be able to wake it up from that sleep, and if there is no more activity, the DE would put it to sleep again...
The big question here of course is, could the DE put it to sleep in the middle of some network transfer?

1. what activity, are you actrually interacting w/ that desktop shell

Trilby wrote:

none that I'm aware of account for network activity, only local mouse and keyboard input

2. Typically yes, the normal idle timers only care whether someboy's typing - not whether your http server is responding to anything.

I was thinking that WoL would be able to wake it up from that sleep

If WoL works, it's gonna wake the system regardless what has triggered that - some desktop idle timer or some traffic monitor calling "systemctl sleep"

But then see cryptearth concern and my previous remark

While the WoL functionality could wake it up via network on request.

You're aware that standard WoL means to explicitly run a command that sends a magic packet to the host that the NIC will take a signal to wake the system.
It does *not* wake just because your trying to access https://mynas/ in a browser, resp. iff the system can wake on physical interaction alone, this will likely cause spurious wakeups (eg. when something pings it or even on broadcast messages)

Offline

Board footer

Powered by FluxBB