#!/bin/bash # tz-drift-check.sh — warn if TIMEZONE (sky-cam.conf / .env) no longer matches # the machine's own timezone. # # systemd timers fire on the machine's clock, but sunrise.py, the capture # scripts and the per-day folder names use TIMEZONE. If the two drift apart # (someone ran `timedatectl set-timezone`, a cloud-init reset, a restored # image, a typo in .env...) every scheduled job runs at the wrong local time # and the sunrise capture window is off by the zone difference. # # Run daily by sky-cam-tzcheck.timer (and shortly after boot). Sends one # notify.sh message per run while the mismatch persists; silent when fine. # Exit 0 either way — a mismatch is a notification, not a job failure. set -uo pipefail SCRIPT_DIR="$(dirname "$(realpath "$0")")" source "$SCRIPT_DIR/sky-cam.conf" source "$SCRIPT_DIR/.env" 2>/dev/null || true # Machine's zone — read WITHOUT any TZ override so we see the real system value. sys_tz="$(env -u TZ timedatectl show -p Timezone --value 2>/dev/null)" [ -z "$sys_tz" ] && sys_tz="$(cat /etc/timezone 2>/dev/null)" [ -z "$sys_tz" ] && sys_tz="$(readlink /etc/localtime 2>/dev/null | sed 's|.*/zoneinfo/||')" if [ -z "$sys_tz" ]; then echo "tz-drift-check: could not determine the machine's timezone — skipping" exit 0 fi # Compare by real UTC offsets across the year so aliases (US/Eastern vs # America/New_York) and identical-rule zones are not reported as drift. same=true if [ "$sys_tz" != "$TIMEZONE" ]; then for m in 01 04 07 10; do a="$(TZ="$sys_tz" date -d "2026-$m-15 12:00 UTC" +%z)" b="$(TZ="$TIMEZONE" date -d "2026-$m-15 12:00 UTC" +%z)" [ "$a" = "$b" ] || { same=false; break; } done fi if $same; then echo "Timezone OK: sky-cam TIMEZONE=$TIMEZONE, machine=$sys_tz" exit 0 fi msg="sky-cam TIMEZONE is $TIMEZONE but this machine's timezone is $sys_tz. Scheduled jobs run on the machine clock, so sunrise/seasons/moon jobs will fire at the wrong local time. Fix: set TIMEZONE=$sys_tz in $SCRIPT_DIR/.env and re-run ./install.sh, or run: sudo timedatectl set-timezone $TIMEZONE" echo "WARNING: $msg" "$SCRIPT_DIR/notify.sh" "WARNING: sky-cam timezone mismatch" "$msg" || true exit 0