Package: prevent-sleep
Linux systemd service to prevent a server from going to sleep when there are (active) clients.Supports SSH and NFS clients.
Uses D-Bus inhibit to prevent sleep/suspend.Note:
For full functionality you must run this as root to allow access to /proc/.... in order to determine if SSH sessions are active or idle.
Depending on Linux configuration, access to the system D-Bus may also require root. In this case the programmustbe run as root.
Installation instructions are for a service running as root.Tested on Fedora 39 with NFSv4.InstallationInstall OS package dependenciessudodnfinstallpython3-pystemdCreate a python virtual environment and installprevent-sleepcd/opt
sudopython-mvenvprevent_sleep--system-site-packages--upgrade-deps
sudoprevent_sleep/bin/python-mpipinstall--upgradeprevent-sleep# or use path to sourcedirprevent_sleep/bin/prevent-sleep--versionSetup systemd servicesudoprevent_sleep/bin/prevent-sleep--install-service
sudosystemctldaemon-reload
sudosystemctlenable--nowprevent-sleepVerifyNote: Usesystemd-inhibitto check if this or other programs are preventing suspend.[xxx@yyy opt]$ systemd-inhibit
WHO            UID USER PID  COMM           WHAT              WHY                                                           MODE
ModemManager   0   root 1197 ModemManager   sleep             ModemManager needs to reset devices                           delay
NetworkManager 0   root 1239 NetworkManager sleep             NetworkManager needs to turn off networks                     delay
UPower         0   root 1794 upowerd        sleep             Pause device polling                                          delay
prevent-sleep  0   root 4678 prevent-sleep  sleep             SSH: 2 active clients [(3567, 'john'), (4007, 'jane')]        block
gdm            42  gdm  1846 gsd-power      handle-lid-switch External monitor attached or configuration changed recently   block
gdm            42  gdm  1846 gsd-power      sleep             GNOME needs to lock the screen                                delay

6 inhibitors listed.[xxx@yyy opt]$ systemd-inhibit
WHO            UID USER PID  COMM           WHAT              WHY                                                             MODE
ModemManager   0   root 1197 ModemManager   sleep             ModemManager needs to reset devices                             delay
NetworkManager 0   root 1239 NetworkManager sleep             NetworkManager needs to turn off networks                       delay
UPower         0   root 1794 upowerd        sleep             Pause device polling                                            delay
prevent-sleep  0   root 4678 prevent-sleep  sleep             NFS: 1 clients [('4', '192.168.4.107, Linux NFSv4.2 mylaptop')] block
prevent-sleep  0   root 4678 prevent-sleep  sleep             SSH: No activity. Will remove block at 2024-01-28T19:36:51      block
gdm            42  gdm  1846 gsd-power      handle-lid-switch External monitor attached or configuration changed recently     block
gdm            42  gdm  1846 gsd-power      sleep             GNOME needs to lock the screen                                  delay

7 inhibitors listed.Note that only SSH clients with traffic during the last check period will prevent sleep!Developmentsudodnfinstallsystemd-develpython3{.10,.11,.12}-devel# For all python versions listen in noxfile.pypipinstallnox
nox# In root of checked out sourcesNote that the test is rather rudimentary. It isnotmocked (yet) so running it will require D-Bus access and it will fail if
being run on a server with NFS clients (if not run as root).MoreThis program [https://pypi.org/project/sleep-inhibitor/], which I found after writingprevent-sleep, does a similar thing.