Skip to main content

Systemd-Service

Simple API to automate the creation of custom daemons for GNU/Linux.

GitHub top language PyPI - License PyPI PyPI - Status PyPI - Python Version GitHub last commit CodeFactor Grade Documentation Status

A daemon is a service process that runs in the background and supervises the system or provides functionality to other processes. Traditionally, daemons are implemented following a scheme originating in SysV Unix. Modern daemons should follow a simpler yet more powerful scheme, as implemented by systemd.

Systemd-Service is a Python module to automate the creation of daemons under GNU/Linux environments.

Install

pip install -U systemd-service

Handle daemons

from systemd_service import Service

daemon = Service("stream_rpyc")

daemon.stop()     # Start (activate) the unit.
daemon.start()    # Stop (deactivate) the unit.
daemon.reload()   # Reload the unit.  
daemon.restart()  # Start or restart the unit.

daemon.enable()   # Enable the unit.
daemon.disable()  # Disable the unit.

daemon.remove()   # Remove the file unit.

This commands are uquivalent to the systemctl calls, for example run in terminal the folowing command: $ systemctl enable stream_rpyc Can be running inside a Python environment with using systemd_service

from systemd_service import Service

daemon = Service("stream_rpyc")
daemon.enable()

Creating services

Similar to the previous scripts, the services can be created using systemd_service:

daemon = Service("stream_rpyc")
daemon.create_service()

If the service must be initialized after other service

daemon = Service("stream_rpyc")
daemon.create_service(after='ntpd')

Creating timers

Defines a timer relative to when the machine was booted up:

daemon = Service("stream_rpyc")
daemon.create_timer(on_boot_sec=15)

Example

This module is useful when is combined with package scripts declaration in setup.py file:

# setup.py

scripts=[
    "cmd/stream_rpyc",
]

The script could looks like:

#!/usr/bin/env python

import sys

if sys.argv[-1] == "systemd":
    from systemd_service import Service
    daemon = Service("stream_rpyc")
    daemon.create_timer(on_boot_sec=10, after='network.target kafka.service')

else:
    from my_module.submodule import my_service
    print("Run 'stream_rpyc systemd' as superuser to create the daemon.")
    my_service()

Then the command can be called as a simple script but with the systemd argument the command will turn into a service.

$ stream_rpyc
# Command executed normally
$ stream_rpyc systemd
# Service created

Metadata

Release files for systemd-service 1.8

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

Source distribution (sdist)

Source distribution for systemd-service 1.8
File Size Uploaded
systemd_service-1.8.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for systemd-service 1.8
File Interpreter ABI Platform
systemd_service-1.8-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / systemd_service-1.8.tar.gz

Download URL systemd_service-1.8.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3c53a0180c7ebe6b62461c3218816d2d3f87050f058c0f3281006e7fac301e19
BLAKE2b-256 checksum
How to use checksums
8ae92e1753130e184e38750cc6e4c3186f2c7421418e42c012ab25a61c14e704
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / systemd_service-1.8-py3-none-any.whl

Download URL systemd_service-1.8-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c818669d3e28188ecbe1911c7651e710eaa2604497a0ba5d943ffe5aa242d9f7
BLAKE2b-256 checksum
How to use checksums
e52fd8bae2205586aa298439f5a8991f7a245c9ffdd239d1dec20fa3d8c2dcf6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release history Release notifications | RSS feed

This release

1.8 This release

2 release files

1.6

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

1 release file

0.9

1 release file

0.8

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

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