Files
ansible/roles/backup_server/files/backup-disk-onoff.py

39 lines
1.0 KiB
Python

import subprocess
import json
# Run the systemctl command and capture the output
try:
result = subprocess.run(
["systemctl", "list-units", "--type=automount", "--no-legend", "--output=json"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True
)
except subprocess.CalledProcessError as e:
print(f"Error executing command: {e.stderr}")
exit(1)
# Parse the JSON output
try:
units = json.loads(result.stdout)
except json.JSONDecodeError as e:
print(f"Error parsing JSON: {e}")
exit(1)
# Filter units that are loaded, active, and sub: running
running_units = []
for unit in units:
if (
unit.get("load") == "loaded" and
unit.get("active") == "active" and
unit.get("sub") == "running" and
unit.get("unit", "").startswith("backup-")
):
running_units.append(unit)
# Output the results
print("Units with load: loaded, active: active, sub: running:")
for unit in running_units:
print(f" - {unit['unit']}: {unit['description']}")