Skip to main content

Systemd service handler

Project description

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

Project details


Release history Release notifications | RSS feed

This version

0.9

Download files

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

Source Distribution

servicehandler-0.9.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

servicehandler-0.9-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file servicehandler-0.9.tar.gz.

File metadata

  • Download URL: servicehandler-0.9.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for servicehandler-0.9.tar.gz
Algorithm Hash digest
SHA256 b33eb2b11b2b047217746b698650f97f33b1161f761275fff7978adc39bb95b9
MD5 8b404a983928b22c3eff8cdd08cd77ee
BLAKE2b-256 fbdb4a759f2056c001d5b38f1a5d4cd82bdf2b81b8119a70e531f0c31554be07

See more details on using hashes here.

File details

Details for the file servicehandler-0.9-py3-none-any.whl.

File metadata

  • Download URL: servicehandler-0.9-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for servicehandler-0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 a5fac0b9b3f90756c13a5f6c385cbbc5b2d8ba9de497b7824a9a02539cdcc57d
MD5 78997f5da87cc477b881864c4598b946
BLAKE2b-256 10563f9c26cfc98e0e7773c559fbc979b8bccbfca40f5cf832e84f504d1c6e6a

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