#!/usr/bin/env bash

# Some examples on how to list and iterate over systemd units (e.g. services or timers).
# With an additional example on how to check if one of multiple services is running.

function all_timers(){
  # Newline separated list of systemd timers.
  timers="$(systemctl list-units --type=timer --plain --quiet | awk '{ print $1 }')"
  # For $timer in $timers.
  while IFS= read -r timer; do
    echo "Timer: ${timer}"
  done <<< "${timers}"
}

function btrfs_scrub_timers(){
  # Newline separated list of systemd timers which start with `btrfs-scrub`.
  timers="$(systemctl list-units --type=timer --plain --quiet | awk '{ print $1 }' | grep '^btrfs-scrub')"
  # For $timer in $timers.
  while IFS= read -r timer; do
    echo "Timer: ${timer}"
  done <<< "${timers}"
}

function running_btrfs_scrub(){
  systemctl list-units --type=service --plain --quiet | awk '{ print $1 }' | grep '^btrfs-scrub'
}

running_btrfs_scrub