Skip to main content

Prevent sleep/suspend when there are (active) SSH or NFS clients.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prevent-sleep-0.1.0.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

prevent_sleep-0.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file prevent-sleep-0.1.0.tar.gz.

File metadata

  • Download URL: prevent-sleep-0.1.0.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for prevent-sleep-0.1.0.tar.gz
Algorithm Hash digest
SHA256 99bbf071cbff9a74fe933b96e4f2cf2a9c5f50c083a0387b077a8a1b9e3a315c
MD5 81c3b59540361bc696273831b066728e
BLAKE2b-256 4dd08de12d1f0dac6059fdc20e8605cdd57435a0a70a22b1c5024f4711345d19

See more details on using hashes here.

File details

Details for the file prevent_sleep-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for prevent_sleep-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b01300ed99c3afe1cdabbff78b4de9407a5d1d746c8d9b761e7f45948db1990e
MD5 e1ea837e71c9f0f009fba933c81cccac
BLAKE2b-256 dab289bd6649e11fe3a4ae350c54ebf8cc22ff576e0aa9ec5443cb0d8d871905

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page