https://github.com/albertosantagostino/systemd-servicehandler
Python package to handle and orchestrate systemd services
https://github.com/albertosantagostino/systemd-servicehandler
linux python python-library service systemd systemd-service
Last synced: 2 months ago
JSON representation
Python package to handle and orchestrate systemd services
- Host: GitHub
- URL: https://github.com/albertosantagostino/systemd-servicehandler
- Owner: albertosantagostino
- License: mit
- Created: 2020-03-05T08:41:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-27T10:28:11.000Z (almost 6 years ago)
- Last Synced: 2025-10-27T09:26:36.974Z (7 months ago)
- Topics: linux, python, python-library, service, systemd, systemd-service
- Language: Python
- Homepage: https://pypi.org/project/servicehandler/
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

`servicehandler` is an orchestrator for systemd services, distributed as a Python package. It provides abstraction for services and implements helper methods, wrapping the `systemctl` command
Using the package does **not** require root permissions, as the user service manager is used (with unit files in `/usr/lib/systemd/user/`)
## Description
A service (aka *daemon*) is defined by its **unit file** (e.g. `/usr/lib/systemd/user/my-service.service`):
```ini
[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
```
### Usage
**Create a ServiceHandler object**
```python
import servicehandler as sh
my_service = sh.ServiceHandler('MyService','my-service.service')
```
**Control the state of a service**
```python
# Check current state
my_service.state()
# Start the service
> my_service.start()
MyService changed state to ServiceState.RUNNING
# Try to start again the service
> my_service.start()
# Terminate the service
> my_service.stop()
MyService changed state to ServiceState.STOPPED
# Kill the service
# In this specific case, the unit file was configured with restart=on-failure (automatic restart)
> my_service.kill()
```
**Control the enablement_state of a service (whether it automatically starts on system startup)**
```python
# Check current enablement_state
> my_service.enablement_state()
# 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
# 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
```
**Iterate over different services and perform batch operations**
```python
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 srv in services:
if srv.state() == sh.ServiceStatus.STOPPED:
srv.restart()
print(srv.state())
```
## Installation
### Install using pip
`servicehandler` is available on [PyPI](https://pypi.org/project/servicehandler/) and it can be installed using **pip**:
```
pip install servicehandler
```
### Build from source
As an alternative, it's possible 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 package was developed while working on a Telegram bot ~~overlord~~ manager, created to handle other bots (and services) running on the same platform, providing a single point of access to the user
In the 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 newly created package
## License
The package is distributed under the [MIT License](https://opensource.org/licenses/MIT)