
function recycle{
start_hour=$(date +%H)

if (( current_minute + 5 >= 60 )); then

  start_hour=$(( (start_hour + 1) % 24 )) # handle wrap-around at 24 hours

fi

start_day=$(date +%d)
start_month=$(date +%m)
start_weekday=$(date +%w)


# Get external IP and hostname (using a reliable method)

external_ip=$(curl -s ifconfig.me)  # Or use dig +short myip.opendns.com

hostname=$(hostname -f)


# Construct the cron command with the calculated time and logging
cron_command="$start_minute $start_hour $start_day $start_month $start_weekday python3 /yankee-gnome-fire-consumer/consumer.py >> /var/log/yankee-downloader.log 2>&1"

# Add IP and hostname to log message *before* the python script runs
log_message="Starting consumer at $(date) from $hostname ($external_ip): "

# Add the log message to the log file, then run the python script and redirect stderr
# This is done using a subshell to ensure the log message comes before the python script's output.
cron_command_with_log="echo \"$log_message\" >> /var/log/yankee-downloader.log 2>&1; $cron_command"


# Add the cron job (using crontab -l to get existing entries, adding the new one, and then setting the crontab)
(crontab -l; echo "$cron_command_with_log") | crontab -


echo "Cron job added: $cron_command_with_log"