From aa6f6fa0b53ac3e2d33940fc11eb4537f3a2b643 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sun, 16 Jun 2024 00:51:25 -0500 Subject: [PATCH] renamed cleanup_services.py --- README.md | 7 +++++++ cleanup_services.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ cleanup_services.sh | 6 ------ 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100755 cleanup_services.py delete mode 100755 cleanup_services.sh diff --git a/README.md b/README.md index 120495b..bff7455 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,13 @@ Use the `install_service.py` script to install. > [!CAUTION] > `example.service` does not stop when disconnecting from `/run/example.sock` +## Scripts + +- `install_service.py`: Installs the services on the system +- `run_service.py`: Used to demo running the code tunnel from a python function +- `test_socket.py`: Opens a connection to the socket +- `cleanup_services.sh`: Stops services and removes symlinks + ## Other Commands Reload systemd units diff --git a/cleanup_services.py b/cleanup_services.py new file mode 100755 index 0000000..37b6595 --- /dev/null +++ b/cleanup_services.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +# for service in $(systemctl list-units --type=service --no-legend | grep example | awk '{print $1}'); do +# sudo systemctl stop $service +# echo "Stopped $service" +# done + +import subprocess +from pathlib import Path +from typing import List + + +def list_units() -> List[str]: + result = subprocess.run( + ['systemctl', 'list-units', '--type=service', '--no-legend'], + check=True, + text=True, + capture_output=True, + ) + return [line.split()[0] for line in result.stdout.splitlines()] + + +def stop_service(service: str): + result = subprocess.run(['sudo', 'systemctl', 'stop', service]) + if result.returncode == 0: + print(f'Stopped {service}') + return result.returncode + + +def cleanup_service(service: str): + stop_result = stop_service(service) + + service_file = Path('/etc/systemd/system') / service + if service_file.exists(): + service_file.unlink() + + if stop_result == 0: + subprocess.run(['sudo', 'systemctl', 'daemon-reload'], check=True) + print('Reloaded services') + + +if __name__ == '__main__': + names = list_units() + for service in ['example.service', 'example.socket']: + cleanup_service(service) diff --git a/cleanup_services.sh b/cleanup_services.sh deleted file mode 100755 index 2d0e45e..0000000 --- a/cleanup_services.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -for service in $(systemctl list-units --type=service --no-legend | grep example | awk '{print $1}'); do - sudo systemctl stop $service - echo "Stopped $service" -done