From 5546d5511e0d4936b01288bf172d9b5c339ec970 Mon Sep 17 00:00:00 2001 From: chiko Date: Wed, 22 Oct 2025 19:10:04 +0200 Subject: [PATCH] replaced /etc/environment with /etc/cron-env.sh to work with cron --- docker/Crontab | 7 +++++-- docker/docker-entrypoint.sh | 38 +++++++++++++++++++++++++++---------- run-task.sh | 13 ++++++------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/docker/Crontab b/docker/Crontab index ef428f5..38d1887 100644 --- a/docker/Crontab +++ b/docker/Crontab @@ -1,2 +1,5 @@ -0 8 * * * root /opt/app/run-task.sh --today -*/15 * * * * root /opt/app/run-task.sh +SHELL=/bin/bash +MAILTO="" +0 8 * * * root . /etc/cron-env.sh && /opt/app/run-task.sh --today >> /proc/1/fd/1 2>&1 +*/15 * * * * root . /etc/cron-env.sh && /opt/app/run-task.sh >> /proc/1/fd/1 2>&1 +* * * * * root echo "cron test ran at $(date)" >> /proc/1/fd/1 2>&1 diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index ab691c8..4138e55 100644 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -1,15 +1,33 @@ #!/bin/bash -bun run ./src/app.ts --today -echo "ntfy_on=$ntfy_on" >> /etc/environment -echo "ntfy_username=$ntfy_username" >> /etc/environment -echo "ntfy_password=$ntfy_password" >> /etc/environment -echo "ntfy_host=$ntfy_host" >> /etc/environment -echo "ntfy_topic=$ntfy_topic" >> /etc/environment -echo "dc_on=$dc_on" >> /etc/environment -echo "dc_webhook=$dc_webhook" >> /etc/environment -echo "dc_botname=$dc_botname" >> /etc/environment -echo "dc_avatar_url=$dc_avatar_url" >> /etc/environment +# Create or overwrite the cron env file +cat /dev/null > /etc/cron-env.sh +chmod 600 /etc/cron-env.sh +chmod +x /etc/cron-env.sh +# Write the Env Vars into a file for cron. happens during runtime of the container and not build. +# List your environment variables here +env_vars=( + ntfy_on + ntfy_username + ntfy_password + ntfy_host + ntfy_topic + dc_on + dc_webhook + dc_botname + dc_avatar_url +) + +for var in "${env_vars[@]}"; do + val="${!var}" + if [ -n "$val" ]; then + # Safely export the variable with proper quoting + printf 'export %s=%q\n' "$var" "$val" >> /etc/cron-env.sh + fi +done + +export PATH="/root/.bun/bin:$PATH" +bun run /opt/app/src/app.ts --today # Start cron in foreground exec cron -f \ No newline at end of file diff --git a/run-task.sh b/run-task.sh index a5a79cf..dc5e2bb 100644 --- a/run-task.sh +++ b/run-task.sh @@ -1,13 +1,10 @@ #!/bin/bash -set -e - -set -o allexport -. /etc/environment || echo "[WARN] Failed to load env" >> /proc/1/fd/2 -set +o allexport - +# set -e export PATH="/root/.bun/bin:$PATH" -cd /opt/app +set -o allexport +. /etc/cron-env.sh || echo "[WARN] Failed to load env" >> /proc/1/fd/2 +set +o allexport log_info() { echo "[INFO] $(date) $1" >> /proc/1/fd/1 @@ -19,6 +16,8 @@ log_error() { log_info "Starting task with args: $*" +cd /opt/app + if bun run ./src/app.ts "$@" >> /proc/1/fd/1 2>> /proc/1/fd/2; then log_info "Task completed successfully." else