replaced /etc/environment with /etc/cron-env.sh to work with cron

This commit is contained in:
2025-10-22 19:10:04 +02:00
parent 4e21b1372f
commit 5546d5511e
3 changed files with 39 additions and 19 deletions

View File

@@ -1,2 +1,5 @@
0 8 * * * root /opt/app/run-task.sh --today SHELL=/bin/bash
*/15 * * * * root /opt/app/run-task.sh 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

View File

@@ -1,15 +1,33 @@
#!/bin/bash #!/bin/bash
bun run ./src/app.ts --today
echo "ntfy_on=$ntfy_on" >> /etc/environment # Create or overwrite the cron env file
echo "ntfy_username=$ntfy_username" >> /etc/environment cat /dev/null > /etc/cron-env.sh
echo "ntfy_password=$ntfy_password" >> /etc/environment chmod 600 /etc/cron-env.sh
echo "ntfy_host=$ntfy_host" >> /etc/environment chmod +x /etc/cron-env.sh
echo "ntfy_topic=$ntfy_topic" >> /etc/environment # Write the Env Vars into a file for cron. happens during runtime of the container and not build.
echo "dc_on=$dc_on" >> /etc/environment # List your environment variables here
echo "dc_webhook=$dc_webhook" >> /etc/environment env_vars=(
echo "dc_botname=$dc_botname" >> /etc/environment ntfy_on
echo "dc_avatar_url=$dc_avatar_url" >> /etc/environment 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 # Start cron in foreground
exec cron -f exec cron -f

View File

@@ -1,13 +1,10 @@
#!/bin/bash #!/bin/bash
set -e # set -e
set -o allexport
. /etc/environment || echo "[WARN] Failed to load env" >> /proc/1/fd/2
set +o allexport
export PATH="/root/.bun/bin:$PATH" 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() { log_info() {
echo "[INFO] $(date) $1" >> /proc/1/fd/1 echo "[INFO] $(date) $1" >> /proc/1/fd/1
@@ -19,6 +16,8 @@ log_error() {
log_info "Starting task with args: $*" 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 if bun run ./src/app.ts "$@" >> /proc/1/fd/1 2>> /proc/1/fd/2; then
log_info "Task completed successfully." log_info "Task completed successfully."
else else