Skip to main content

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 program must be run as root. Installation instructions are for a service running as root.

Tested on Fedora 39 with NFSv4.

Installation

Install OS package dependencies

sudo dnf install python3-pystemd

Create a python virtual environment and install prevent-sleep

cd /opt
sudo python -m venv prevent_sleep --system-site-packages --upgrade-deps
sudo prevent_sleep/bin/python -m pip install --upgrade prevent-sleep  # or use path to sourcedir
prevent_sleep/bin/prevent-sleep --version

Setup systemd service

sudo prevent_sleep/bin/prevent-sleep --install-service
sudo systemctl daemon-reload
sudo systemctl enable --now prevent-sleep

Verify

Note: Use systemd-inhibit to 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!

Development

sudo dnf install systemd-devel python3{.10,.11,.12}-devel  # For all python versions listen in noxfile.py
pip install nox
nox  # In root of checked out sources

Note that the test is rather rudimentary. It is not mocked (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).

More

This program [https://pypi.org/project/sleep-inhibitor/], which I found after writing prevent-sleep, does a similar thing.

Metadata

Release files for prevent-sleep 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prevent-sleep 0.1.1
File Size Uploaded
prevent_sleep-0.1.1.tar.gz 20.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prevent-sleep 0.1.1
File Interpreter ABI Platform
prevent_sleep-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 33.9 kB

Release files / prevent_sleep-0.1.1.tar.gz

Download URL prevent_sleep-0.1.1.tar.gz
Size 20.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7c7a5b01f624db5bcc5b44b80028c6c2376c1c89f9aecb021716247f71ddfca6
BLAKE2b-256 checksum
How to use checksums
e743e262ed6717bae54d1a520cdb1e82cdddefdca4cb1a90570dee44ef74459e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / prevent_sleep-0.1.1-py3-none-any.whl

Download URL prevent_sleep-0.1.1-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe8dd320f9e1bd889fe8f3a854ed98a37059ac5eab06d8187ccc4930960f3987
BLAKE2b-256 checksum
How to use checksums
0a7feaf01070275a40bf59c0c66dbaca8712e2ffb00d66329503768c1d9b0a62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page