Add running state indicator for systemd timers
Some checks failed
Ansible Lint Check / check-ansible (push) Failing after 9s
Nix Format Check / check-format (push) Failing after 27s
Python Lint Check / check-python (push) Failing after 8s

This commit is contained in:
2025-07-28 23:16:54 +02:00
parent 76c2586a21
commit 7d01d476b1
2 changed files with 45 additions and 10 deletions

View File

@@ -271,6 +271,14 @@ def get_systemd_timer_status(timer_name):
text=True
)
# Check corresponding service status
service_name = timer_name.replace('.timer', '.service')
service_result = subprocess.run(
["sudo", "systemctl", "is-active", service_name],
capture_output=True,
text=True
)
# Get next run time
list_result = subprocess.run(
["sudo", "systemctl", "list-timers", timer_name, "--no-legend"],
@@ -280,6 +288,7 @@ def get_systemd_timer_status(timer_name):
is_active = active_result.returncode == 0
is_enabled = enabled_result.returncode == 0
service_status = service_result.stdout.strip() if service_result else "unknown"
next_run = "unknown"
if list_result.returncode == 0 and list_result.stdout.strip():
@@ -287,7 +296,7 @@ def get_systemd_timer_status(timer_name):
if len(parts) >= 4:
next_run = f"{parts[0]} {parts[1]} {parts[2]}"
return is_active, is_enabled, next_run
return is_active, is_enabled, next_run, service_status
def cmd_list(args):
@@ -328,10 +337,14 @@ def cmd_list(args):
systemd_timers = ["borg-backup.timer", "borg-local-sync.timer", "dynamic-dns.timer"]
for timer in systemd_timers:
is_active, is_enabled, next_run = get_systemd_timer_status(timer)
is_active, is_enabled, next_run, service_status = get_systemd_timer_status(timer)
service_name = timer.replace('.timer', '')
if is_active and is_enabled:
if service_status in ["activating", "active"]:
# Service is currently running
status = f"[🔄 RUNNING - next: {next_run}]"
color = "yellow"
elif is_active and is_enabled:
status = f"[TIMER ACTIVE - next: {next_run}]"
color = "green"
elif is_enabled:

View File

@@ -24,7 +24,13 @@ def show_timer_status(timer_name, system_level=True):
# Get timer status
status_cmd = f"{cmd_prefix} is-active {timer_name}"
status_result = run_command(status_cmd)
status = "active" if status_result and status_result.returncode == 0 else "inactive"
timer_status = "active" if status_result and status_result.returncode == 0 else "inactive"
# Get corresponding service status
service_name = timer_name.replace('.timer', '.service')
service_cmd = f"{cmd_prefix} is-active {service_name}"
service_result = run_command(service_cmd)
service_status = service_result.stdout.strip() if service_result else "unknown"
# Get next run time
list_cmd = f"{cmd_prefix} list-timers {timer_name} --no-legend"
@@ -36,14 +42,26 @@ def show_timer_status(timer_name, system_level=True):
if len(parts) >= 4:
next_run = f"{parts[0]} {parts[1]} {parts[2]} ({parts[3]})"
# Get service name
service_name = timer_name.replace('.timer', '.service')
# Format output
status_color = "green" if status == "active" else "red"
# Format output based on service status
service_short = service_name.replace('.service', '')
printfe(status_color, f"{service_short:<12} {status:<8} next: {next_run}")
if service_status in ["activating", "active"]:
# Service is currently running
status_color = "yellow"
status_text = f"RUNNING next: {next_run}"
symbol = "🔄"
elif timer_status == "active":
# Timer is active but service is not running
status_color = "green"
status_text = f"active next: {next_run}"
symbol = ""
else:
# Timer is inactive
status_color = "red"
status_text = f"inactive next: {next_run}"
symbol = ""
printfe(status_color, f"{symbol} {service_short:<12} {status_text}")
def show_examples():
"""Show example commands for checking services and logs"""
@@ -52,17 +70,21 @@ def show_examples():
printfe("yellow", "Check service status:")
print(" sudo systemctl status borg-backup.service")
print(" sudo systemctl status borg-local-sync.service")
print(" sudo systemctl status dynamic-dns.service")
print()
printfe("yellow", "View logs:")
print(" sudo journalctl -u borg-backup.service -f")
print(" sudo journalctl -u borg-local-sync.service -f")
print(" sudo journalctl -u dynamic-dns.service -f")
print(" tail -f /var/log/borg-backup.log")
print(" tail -f /var/log/borg-local-sync.log")
print()
printfe("yellow", "Manual trigger:")
print(" sudo systemctl start borg-backup.service")
print(" sudo systemctl start borg-local-sync.service")
print(" sudo systemctl start dynamic-dns.service")
print()