Skip to main content

Collection of utils for systemctl related tasks

Project description

jps-systemctl-utils

Build Publish to PyPI codecov Python 3.10+ License: MIT

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 โ†’ status for 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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Run tests (make test)
  4. Commit your changes (git commit -m 'Add some AmazingFeature')
  5. Push to the branch (git push origin feature/AmazingFeature)
  6. 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 --dryrun first
  • Review the service list carefully
  • Have appropriate system permissions
  • Back up important configurations
  • Use in controlled environments

Project details


Download files

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

Source Distribution

jps_systemctl_utils-0.1.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jps_systemctl_utils-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file jps_systemctl_utils-0.1.0.tar.gz.

File metadata

  • Download URL: jps_systemctl_utils-0.1.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

Hashes for jps_systemctl_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 39055e9fc87d5cfb8c0ab1358cb4bd981d9f95d97d1683456377d5af8788a8fa
MD5 5da7f196799d1e3e2842bb2c14ff6a11
BLAKE2b-256 b94bfc8c0431238a06e48f8932c257bbe016da277b478a8866c85d8283a03af5

See more details on using hashes here.

File details

Details for the file jps_systemctl_utils-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jps_systemctl_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbf13af17df26a4e98f4e4d3f792d2ccf1c5357ef7d3061777967f7b1401e316
MD5 81e1f62f63b4c4ee8c16a6f24ea26faa
BLAKE2b-256 ff6646f71f08a35db50cc250e4bcca8a2f70397392e0e6788255f2f5043c4a2f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page