Skip to main content

Systemd service handler (servicehandler)

servicehandler is a Python library that provides an orchestrator for systemd services. It abstracts services as objects and implements helper methods, wrapping the systemctl command

Using this package does not require root permissions, as the service manager used is the one of the current user (the service configuration files are in /usr/lib/systemd/user/)

Description

The first thing to handle services is to create a service unit file (in /usr/lib/systemd/user/my-service.service) like the following:

[Unit]
Description=My service

[Service]
ExecStart=/usr/bin/python3 /home/user/service_script/main.py
Environment=PATH=/bin:/usr/bin:/usr/local/bin
WorkingDirectory=/home/user/service_script/

[Install]
WantedBy=multi-user.target

Depending on how you created the service file you may need to provide access to the user through sudo chmod 644 my-service.service

Usage

Control the state of a service

import servicehandler as sh

# Create a new service handler
my_service = sh.ServiceHandler('MyService','my-service.service')

# Check current state
my_service.state()
<ServiceState.STOPPED: 2>

# Start the service
> my_service.start()
MyService changed state to ServiceState.RUNNING
<Response.OK: 1>

# Try to start again the service
> my_service.start()
<Response.ALREADY: 2>

# Terminate the service
> my_service.stop()
MyService changed state to ServiceState.STOPPED
<Response.OK: 1>

# Kill the service
# In this specific case, the unit file was configured with restart=on-failure (automatic restart)
> my_service.kill()
<Response.OK: 1>

Control the enablement_state of a service (whether it starts automatically on system startup)

# Check current enablement_state
> my_service.enablement_state()
<ServiceEnablementState.DISABLED: 2>

# Enable the service
> my_service.enable()
Created symlink /home/user/.config/systemd/user/multi-user.target.wants/my_service.service → /usr/lib/systemd/user/my_service.service.
MyService changed enablement state to ServiceEnablementState.ENABLED
<Response.OK: 1>

# Disable the service
> my_service.disable()
Removed /home/user/.config/systemd/user/multi-user.target.wants/my_service.service.
MyService changed enablement state to ServiceEnablementState.DISABLED
<Response.OK: 1>

Iterate over different services and perform batch operations

import servicehandler as sh

service_A = sh.ServiceHandler('ServiceA','A-config-file.service')
service_B = sh.ServiceHandler('ServiceB','B-config-file.service')
service_C = sh.ServiceHandler('ServiceC','C-config-file.service')

services = [ServiceA, ServiceB, ServiceC]

# Iterate over the services easily
for sr in services:
    if sr.state == sh.ServiceStatus.STOPPED:
        sr.restart()
    print(sr)

Installation

Install using pip

This package is available on PyPI and it can be installed using pip:

pip install servicehandler

Build from source

To build and install the package from source:

git clone https://github.com/albertosantagostino/systemd-servicehandler
cd systemd-servicehandler
python3 setup.py install

Development history and use cases

Manage multiple services from a single entry-point

This library was developed while working on a Telegram bot overlord manager, used to handle other bots (and services) running on the same platform, providing a single point of access to the user

In this scenario multiple bots run on a headless Raspberry Pi Zero. In order to start them when needed, check their logs and interact with them without opening an SSH session every time, a brand new all-powerful Telegram bot was created, weaponized with this new package

License

The package is distributed under the MIT License

Metadata

Release files for servicehandler 0.9

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

Source distribution (sdist)

Source distribution for servicehandler 0.9
File Size Uploaded Provenance Actions
servicehandler-0.9.tar.gz 6.7 kB Without provenance Details for servicehandler-0.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for servicehandler 0.9
File Interpreter ABI Platform Actions
servicehandler-0.9-py3-none-any.whl Python 3 none any Details for servicehandler-0.9-py3-none-any.whl

Total release size: 14.2 kB

Release files / servicehandler-0.9.tar.gz

Download URL servicehandler-0.9.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b33eb2b11b2b047217746b698650f97f33b1161f761275fff7978adc39bb95b9
BLAKE2b-256 checksum
How to use checksums
fbdb4a759f2056c001d5b38f1a5d4cd82bdf2b81b8119a70e531f0c31554be07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

Release files / servicehandler-0.9-py3-none-any.whl

Download URL servicehandler-0.9-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5fac0b9b3f90756c13a5f6c385cbbc5b2d8ba9de497b7824a9a02539cdcc57d
BLAKE2b-256 checksum
How to use checksums
10563f9c26cfc98e0e7773c559fbc979b8bccbfca40f5cf832e84f504d1c6e6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

Security for servicehandler 0.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9 This release

2 release files No provenance

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