https://github.com/t4d-gmbh/systemdunits
Programmatically create systemd unit files and manage them
https://github.com/t4d-gmbh/systemdunits
asyncio python3 systemd systemd-units
Last synced: about 2 months ago
JSON representation
Programmatically create systemd unit files and manage them
- Host: GitHub
- URL: https://github.com/t4d-gmbh/systemdunits
- Owner: t4d-gmbh
- License: gpl-3.0
- Created: 2022-08-10T14:17:08.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-09-12T20:27:57.000Z (almost 3 years ago)
- Last Synced: 2025-03-30T02:51:06.212Z (3 months ago)
- Topics: asyncio, python3, systemd, systemd-units
- Language: Python
- Homepage:
- Size: 66.4 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SystemdUnits
Small package for reading and writing systemd unit files.
## Installation
Fetch the latest version directly from the repository:
pip install --upgrade git+https://github.com/tools4digits/systemdunits.git
## Usage Examples
### Creating systemd units
Create a new target unit template using the default manager (i.e. `--user`),
write it to `~/.config/systemd/user`, start an instance, show the status,
stop it again and clean up:```python
import asyncio
from sysunit import SystemUnit
my_target = SystemUnit('[email protected]')
# some dummy content
my_target.from_dict(dict(
Unit=dict(Description='Target instance %i'),
Install=dict(WantedBy='multi-user.target')
))
# write it to disk
my_target.write()
# reload the systmd daemon (this is an async operation)
asyncio.run(my_target.run.daemon_reload())
# start an instance of this unit
asyncio.run(my_target.run.start(instance='eg1'))
# get the status
out, err = asyncio.run(my_target.run.status(instance='eg1'))
print(out)
# ● [email protected] - Target instance eg1
# Loaded: loaded (.../.config/systemd/user/[email protected]; disabled;
# vendor preset: enabled)
# Active: active since ... ago
# ... systemd[...]: Reached target Target instance eg1.# stop the instance again
asyncio.run(my_target.run.stop(instance='eg1'))
# remove the unit file again
my_target.remove()
```## Development
### Testing
Testing is done with `unittest`, including `doctest`.To run tests simply go to the project root folder and run:
python -m unittest
## Copyright
Copyright © 2022 T4D GmbH