renamed cleanup_services.py

This commit is contained in:
John Lancaster
2024-06-16 00:51:25 -05:00
parent db14acfe2d
commit aa6f6fa0b5
3 changed files with 52 additions and 6 deletions

45
cleanup_services.py Executable file
View File

@@ -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)