From 6eb7918ff4b505d9e5c0d9cfd7eb35ae92050710 Mon Sep 17 00:00:00 2001 From: Pavel Astakhov Date: Mon, 21 Oct 2024 20:09:14 +0500 Subject: [PATCH] Add tzdata to allow changing timezone and use UTC for logging (#5) --- Dockerfile | 2 +- README.md | 3 ++- update_cron.sh | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index f6e3122..70e65d6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ LABEL maintainers="pavel@wikiteq.com" LABEL org.opencontainers.image.source=https://github.com/WikiTeq/docker-cron # Install required tools -RUN apk add --no-cache jq curl docker-cli +RUN apk add --no-cache jq curl docker-cli tzdata # Latest releases available at https://github.com/aptible/supercronic/releases ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.2.32/supercronic-linux-amd64 \ diff --git a/README.md b/README.md index cecc503..22e8a07 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ services: environment: - COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME} - DEBUG=${CRON_DEBUG:-0} + - TZ=America/New_York app: build: ./app @@ -38,4 +39,4 @@ The `example_compose.yml` file uses several environment variables. Make sure to - `COMPOSE_PROJECT_NAME`: if defined, the cron looks for the jobs in this compose project only - `DEBUG`: when it is `true`, it outputs more iformation for debugging - `CRON_LOG_DIR`: the directory where cron puts the log files of the executed jobs, `/var/log/cron` by default - +- `TZ` timezone used for scheduling the cron jobs diff --git a/update_cron.sh b/update_cron.sh index 189c980..566124f 100644 --- a/update_cron.sh +++ b/update_cron.sh @@ -11,7 +11,7 @@ CRON_LOG_DIR=${CRON_LOG_DIR:-/var/log/cron} mkdir -p "$CRON_LOG_DIR" timestamp() { - date "+%Y-%m-%d %H:%M:%S" + date -u "+%Y-%m-%d %H:%M:%S %Z" } # Function to extract specific cron job labels from a container @@ -65,7 +65,7 @@ for container in $containers; do # Check if both schedule and command labels are set if [ -n "$job_schedule" ] && [ -n "$job_command" ]; then target_container=$(docker inspect -f '{{.Name}}' "$container" | cut -c2-) # Remove leading / - cron_entry="$job_schedule docker exec $target_container sh -c '$job_command' 2>&1 | tee -a $CRON_LOG_DIR/\$(date +\%Y-\%m-\%d_\%H-\%M-\%S)-$job_key.log" + cron_entry="$job_schedule docker exec $target_container sh -c '$job_command' 2>&1 | tee -a $CRON_LOG_DIR/\$(date -u +\%Y-\%m-\%d_\%H-\%M-\%S_\%Z)_$job_key.log" echo "$cron_entry" >> $CRON_FILE_NEW # Write in one line to the cron file log "Scheduled task for $target_container: $cron_entry" else