Skip to content

Instantly share code, notes, and snippets.

@athlan
Last active May 24, 2022 21:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save athlan/b6f09977e2f5cf20840ef61ca3cda932 to your computer and use it in GitHub Desktop.
Save athlan/b6f09977e2f5cf20840ef61ca3cda932 to your computer and use it in GitHub Desktop.
Docker cron env variables
* * * * * bash -c ". /root/.env.sh; /app/bin/console some:symfony_task_here &> /tmp/app-jobs-offers-sync-active.log"
FROM XXXX
#Install cron
RUN apt-get update; \
apt-get install -y cron; \
apt-get clean; \
touch /var/log/cron.log
COPY crontab.txt /etc/cron.d/crontab
COPY run-cron.sh /usr/local/app/entrypoint/run-cron.sh
CMD ["/usr/local/app/entrypoint/run-cron.sh"]
#!/bin/bash
ENV_VARS_FILE="/root/.env.sh"
echo "Dumping env variables into ${ENV_VARS_FILE}"
printenv | sed 's/^\(.*\)$/export \1/g' > ${ENV_VARS_FILE}
chmod +x ${ENV_VARS_FILE}
echo "Applying crontab"
crontab /etc/cron.d/crontab
echo "Running crontab"
cron -f
@amulcse
Copy link

amulcse commented Dec 2, 2020

printenv | sed 's/^(.*)$/export \1/g' > ${ENV_VARS_FILE}

This will be failed when there are any special characters in your environment variable values.

I suggest using the following piece of code to export environment variables.

eval $(printenv | awk -F= '{print "export " "\""$1"\"""=""\""$2"\"" }' >> /etc/profile)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment