Files
dotfiles/bin/actions/timers.py
Menno van Leeuwen 76c2586a21
Some checks failed
Ansible Lint Check / check-ansible (push) Failing after 12s
Nix Format Check / check-format (push) Failing after 25s
Python Lint Check / check-python (push) Failing after 8s
Add Borg local sync system service and configuration
2025-07-28 23:15:49 +02:00

101 lines
3.0 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import subprocess
import sys
# Add the helpers directory to the path
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'helpers'))
from functions import printfe
def run_command(cmd, capture_output=True):
"""Run a command and return the result"""
try:
result = subprocess.run(cmd, shell=True, capture_output=capture_output, text=True)
return result
except Exception as e:
printfe("red", f"Error running command: {e}")
return None
def show_timer_status(timer_name, system_level=True):
"""Show concise status for a specific timer"""
cmd_prefix = "sudo systemctl" if system_level else "systemctl --user"
# 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"
# Get next run time
list_cmd = f"{cmd_prefix} list-timers {timer_name} --no-legend"
list_result = run_command(list_cmd)
next_run = "unknown"
if list_result and list_result.returncode == 0 and list_result.stdout.strip():
parts = list_result.stdout.strip().split()
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"
service_short = service_name.replace('.service', '')
printfe(status_color, f"{service_short:<12} {status:<8} next: {next_run}")
def show_examples():
"""Show example commands for checking services and logs"""
printfe("cyan", "=== Useful Commands ===")
print()
printfe("yellow", "Check service status:")
print(" sudo systemctl status borg-backup.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 dynamic-dns.service -f")
print(" tail -f /var/log/borg-backup.log")
print()
printfe("yellow", "Manual trigger:")
print(" sudo systemctl start borg-backup.service")
print(" sudo systemctl start dynamic-dns.service")
print()
printfe("yellow", "List all timers:")
print(" sudo systemctl list-timers")
print()
def main():
"""Main timers action"""
args = sys.argv[1:] if len(sys.argv) > 1 else []
printfe("cyan", "🕐 System Timers")
print()
# Show timer statuses
timers = [
("borg-backup.timer", True),
("borg-local-sync.timer", True),
("dynamic-dns.timer", True)
]
for timer_name, system_level in timers:
if os.path.exists(f"/etc/systemd/system/{timer_name}"):
show_timer_status(timer_name, system_level)
else:
printfe("yellow", f" {timer_name.replace('.timer', ''):<12} not found")
print()
# Show helpful examples
show_examples()
return 0
if __name__ == "__main__":
sys.exit(main())