1

I'm trying to build a dynamic cron job using as basis this link: How to run a cron job inside a docker container When I'm the container $var1 prints the right value, but seens like cron doesnt like env variables.... Any way to go around this? Dockerfile:

FROM alpine:latest
RUN apk update
RUN apk add rsync run-parts curl
COPY /Scripts/* /
RUN chmod 755 /entry.sh /script.sh /start.sh
#RUN /usr/bin/crontab /crontab.txt
CMD ["/entry.sh"]

entry.sh:

#!/bin/sh
# start cron
sh start.sh
/usr/sbin/crond -f -l 8

start.sh:

#!/bin/bash

/usr/bin/crontab /crontab.txt

docker-compose.yml:

version: '3.6'
services:
    test:
        image: test1
        environment:
            - var1=${var1_env}
    

.env:

var1_env=*/1

crontab.txt:

$var1 * * * * /script.sh >> /var/log/script.log

1 Answers1

0

Cronjobs traditionally run in a fresh, clean environment. The behavior you're seeing is that the cron daemon is 'cleaning up' the environment before running your job.

You have two options:

  1. Have the cronjob run a shell script. That shell script sets up the environment, then runs the command. https://stackoverflow.com/a/2229861/530160
  2. Define the variables inside the crontab. https://stackoverflow.com/a/10657111/530160
Nick ODell
  • 15,465
  • 3
  • 32
  • 66