Collection of utils for systemctl related tasks
Project description
jps-systemctl-utils
A Python utility for automating systemctl service management operations with comprehensive logging and reporting.
๐ Overview
jps-systemctl-utils provides a simple, sequential utility to manage systemd services in bulk. It automates the common workflow of checking status, stopping, starting, and verifying services, with full logging and detailed reports.
Features
- โ Bulk Service Management - Process multiple services from a simple text file
- ๐ Automated Workflow - Runs
status โ stop โ start โ statusfor each service - ๐ Progress Tracking - Real-time progress bar using Rich
- ๐ Comprehensive Logging - Dual logging to file (INFO) and stderr (WARNING+)
- ๐ Detailed Reports - Timestamped reports with all command outputs
- ๐งช Dry-run Mode - Test your workflow without executing commands
- โ๏ธ Flexible Configuration - Custom output directories, log files, and reports
- ๐ฏ Comment Support - Skip comments and blank lines in service files
Use Cases
- Restarting multiple services after configuration changes
- Performing routine service maintenance
- Testing service restart procedures
- Documenting service states before/after operations
- Automating deployment workflows
๐ฆ Installation
From Source
git clone https://github.com/jai-python3/jps-systemctl-utils.git
cd jps-systemctl-utils
make install
For Development
make install-dev-tools
๐ฏ Usage
Basic Usage
Create a file with service names (one per line):
# services.txt
nginx
apache2
mysql
postgresql
Run the utility:
jps-systemctl-utils-restart-services --infile services.txt
Dry-run Mode
Test without executing commands:
jps-systemctl-utils-restart-services --infile services.txt --dryrun
Custom Output Location
jps-systemctl-utils-restart-services --infile services.txt --outdir /var/log/service-ops
Full Control
jps-systemctl-utils-restart-services \
--infile services.txt \
--outdir /custom/output \
--report-file /custom/report.txt \
--logfile /custom/app.log \
--dryrun
Example Output Structure
Default output location: /tmp/<username>/systemctl_runner/<timestamp>/
/tmp/myuser/systemctl_runner/2025-12-13-143000/
โโโ systemctl_runner_report.txt # Detailed command outputs
โโโ systemctl_runner.log # Application logs
Report Format
The generated report includes:
## method-created: /path/to/systemctl_runner.py
## date-created: 2025-12-13 14:30:00
## created-by: myuser
## logfile: /tmp/myuser/systemctl_runner/2025-12-13-143000/systemctl_runner.log
## infile: /path/to/services.txt
# Service: nginx
$ systemctl status nginx
โ nginx.service - A high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service)
Active: active (running)
(exit: 0)
$ systemctl stop nginx
(exit: 0)
$ systemctl start nginx
(exit: 0)
$ systemctl status nginx
โ nginx.service - A high performance web server
Active: active (running)
(exit: 0)
๐ API Documentation
Command-line Options
| Option | Type | Required | Description |
|---|---|---|---|
--infile |
Path | Yes | Text file with newline-separated service names |
--outdir |
Path | No | Output directory for logs and reports |
--report-file |
Path | No | Custom path for the report file |
--logfile |
Path | No | Custom path for the log file |
--dryrun |
Flag | No | If set, no systemctl commands are executed |
Input File Format
# Lines starting with # are comments
nginx
apache2
# Blank lines are ignored
mysql
๐งช Development
Running Tests
# Run all tests with coverage
make test
# Run specific test file
pytest tests/test_processor.py -v
# Run with detailed coverage report
pytest tests/ -v --cov=src/jps_systemctl_utils --cov-report=html
Code Quality
# Auto-fix code issues
make fix
# Format code with black
make format
# Run linting
make lint
# Run all quality checks
make fix && make format && make lint
Pre-commit Hooks
# Install pre-commit hooks
make precommit
Building and Publishing
# Build distribution packages
make build
# Check built packages
make check-build
# Publish to PyPI (with dry-run option)
make publish DRYRUN=1
make publish
๐๏ธ Project Structure
jps-systemctl-utils/
โโโ src/
โ โโโ jps_systemctl_utils/
โ โโโ __init__.py # Package version
โ โโโ constants.py # Logging constants
โ โโโ logging_helper.py # Logging configuration
โ โโโ processor.py # Core service processing logic
โ โโโ report_writer.py # Report generation
โ โโโ systemctl_runner.py # CLI entry point
โโโ tests/
โ โโโ conftest.py # Shared fixtures
โ โโโ test_constants.py # Constants tests
โ โโโ test_logging_helper.py # Logging tests
โ โโโ test_processor.py # Processor tests
โ โโโ test_report_writer.py # Report writer tests
โ โโโ test_systemctl_runner.py # CLI tests
โโโ pyproject.toml # Project metadata
โโโ Makefile # Development commands
โโโ README.md # This file
๐งฉ Requirements
- Python 3.10+
- typer >= 0.12.3
- rich >= 13.0.0
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Run tests (
make test) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐ Changelog
See CHANGELOG.md for version history.
๐ License
MIT License ยฉ Jaideep Sundaram
๐ Links
โ ๏ธ Disclaimer
This tool executes systemctl commands that affect system services. Always:
- Test with
--dryrunfirst - Review the service list carefully
- Have appropriate system permissions
- Back up important configurations
- Use in controlled environments
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jps_systemctl_utils-0.2.0.tar.gz.
File metadata
- Download URL: jps_systemctl_utils-0.2.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ef8b74a0e9307704bf7ba546af232e0d5cd3268358d8920da2bb348c6ea4e7a
|
|
| MD5 |
a6cd95e34e435fb163996182abcc0a1e
|
|
| BLAKE2b-256 |
8d5233c6b5bef8e8334876abdb704a5ceebd4d4d5aa0af592ddec7ea0fd9d61e
|
File details
Details for the file jps_systemctl_utils-0.2.0-py3-none-any.whl.
File metadata
- Download URL: jps_systemctl_utils-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61e4818d10798f9382f6da1dd0d139f43c1ff382dcf4675317ad9202b6b33a9f
|
|
| MD5 |
cf5b0719f03c9f64a8311117ae0cb0fe
|
|
| BLAKE2b-256 |
68a754598dbe5dc32c34ed8a554ad94a4af258fefbb6f5d9f2c11f5092df0141
|