#!/bin/sh set -e # --- Environment Variable Defaults --- PUID=${PUID:-1000} PGID=${PGID:-1000} DOMAIN=${DOMAIN:-} RULE_PATH=${RULE_PATH:-} # --- Create user and group at runtime --- echo "Creating user and group with PUID=${PUID} and PGID=${PGID}" # Remove existing user/group if they exist if getent group app > /dev/null 2>&1; then delgroup app 2>/dev/null || true fi if getent passwd app > /dev/null 2>&1; then deluser app 2>/dev/null || true fi # Create new group and user with runtime PUID/PGID addgroup -S -g ${PGID} app adduser -S -u ${PUID} -G app -h /app app # Set ownership of directories chown -R app:app /config /rules # --- Build the Command --- # This command is now used for both the initial run and the cron job. APP_CMD="/usr/local/bin/sbrs \ --input-config /config/template.json \ --rules-dir /rules \ --output-config /config/default.json \ --domain ${DOMAIN} \ --rule-path ${RULE_PATH}" # --- Manual Run Mode --- # This allows for on-demand execution without restarting the container. if [ "$1" = "manual" ]; then echo "[$(date +"%Y-%m-%d %H:%M:%S")] Running one-time manual sync..." su-exec app:app sh -c "${APP_CMD}" exit 0 fi # =================================================================== # Initial Synchronization on Container Start # =================================================================== echo "--- Running initial synchronization on container start ---" # Execute the command once immediately. # We run it as the non-root 'app' user to ensure correct file permissions. su-exec app:app sh -c "${APP_CMD}" echo "--- Initial synchronization finished ---" # =================================================================== # --- Cron Setup --- # This section runs after the initial sync is complete. echo "Setting up cron job with schedule: ${CRON_SCHEDULE}" crontab -d || true echo "${CRON_SCHEDULE} su-exec app:app sh -c '${APP_CMD}' > /proc/1/fd/1 2>/proc/1/fd/2" | crontab - # --- Start Cron Daemon --- # This must be the last command. It keeps the container running. echo "Starting cron daemon..." exec crond -f -l 8