diff --git a/battybirdnet-pi/rootfs/custom-services.d/30-monitoring.sh b/battybirdnet-pi/rootfs/custom-services.d/30-monitoring.sh index 343a60471..9e9b37781 100755 --- a/battybirdnet-pi/rootfs/custom-services.d/30-monitoring.sh +++ b/battybirdnet-pi/rootfs/custom-services.d/30-monitoring.sh @@ -1,28 +1,30 @@ #!/usr/bin/env bash # shellcheck shell=bash +# Adapted from https://github.com/mcguirepr89/BirdNET-Pi/issues/393#issuecomment-1166445710 + +# Define logging functions +log_green() { echo -e "\033[32m$1\033[0m" } +log_red() { echo -e "\033[31m$1\033[0m" } +log_yellow() { echo -e "\033[33m$1\033[0m" } +log_info() { echo -e "\033[34m$1\033[0m" } + +echo "$(log_green "Starting service: throttlerecording")" +touch "$HOME/BirdSongs/StreamData/analyzing_now.txt" # Read configuration set -u source /config/birdnet.conf 2>/dev/null set +u -# Define logging functions -log_green() { echo -e "\033[32m$1\033[0m"; } -log_red() { echo -e "\033[31m$1\033[0m"; } -log_yellow() { echo -e "\033[33m$1\033[0m"; } -log_info() { echo -e "\033[34m$1\033[0m"; } - -log_green "Starting service: throttlerecording" -touch "$HOME/BirdSongs/StreamData/analyzing_now.txt" - # Set constants srv="birdnet_recording" srv2="birdnet_analysis" ingest_dir="$RECS_DIR/StreamData" counter=10 - # Ensure directories and permissions -mkdir -p "$ingest_dir" && chown -R pi:pi "$ingest_dir" && chmod -R 755 "$ingest_dir" +mkdir -p "$ingest_dir" +chown -R pi:pi "$ingest_dir" +chmod -R 755 "$ingest_dir" # Function to send notifications using Apprise apprisealert() { @@ -77,20 +79,25 @@ while true; do log_red "$(date) WARNING: Too many files in queue, pausing $srv and restarting $srv2" sudo systemctl stop "$srv" sudo systemctl restart "$srv2" - [[ -s "$HOME/BirdNET-Pi/apprise.txt" ]] && apprisealert + sleep 30 elif ((wav_count > 30)); then log_red "$(date) WARNING: Too many files in queue, restarting $srv2" sudo systemctl restart "$srv2" - [[ -s "$HOME/BirdNET-Pi/apprise.txt" ]] && apprisealert - else - if [[ "$service_state" != "active" ]]; then - log_yellow "$(date) INFO: Restarting $srv service" - sudo systemctl restart "$srv" - fi - if [[ "$analysis_state" != "active" ]]; then - log_yellow "$(date) INFO: Restarting $srv2 service" - sudo systemctl restart "$srv2" + sleep 30 + fi + + # Check service states + for service in "$srv" "$srv2"; do + local state_var="${service}_state" + if [[ "${!state_var}" != "active" ]]; then + log_yellow "$(date) INFO: Restarting $service service" + sudo systemctl restart "$service" fi + done + + # Send alert if needed + if ((wav_count > 30)) && [[ -s "$HOME/BirdNET-Pi/apprise.txt" ]]; then + apprisealert fi ((counter--))