I'm setting up a docker container which requires a cronjob to do a backup using awscli
.
I'm having a problem with the cron job being able to access the environment variables of the docker container. As I work around on startup I print all environment variables to a file printenv > /env
.
When I try to use source
from the cron job (I have tried both directly in crontab and in a script called by crontab) it doesn't seem to work.
I made a simplified version of my project to demonstrate the issue (including rsyslog
for logging):
Dockerfile:
FROM debian:jessie
# Install aws and cron
RUN apt-get -yqq update
RUN apt-get install -yqq awscli cron rsyslog
# Create cron job
ADD crontab /etc/cron.d/hello-cron
RUN chmod 0644 /etc/cron.d/hello-cron
# Output environment variables to file
# Then start cron and watch log
CMD printenv > /env && cron && service rsyslog start && tail -F /var/log/*
crontab:
# Every 3 minutes try to source /env and run `aws s3 ls`.
*/3 * * * * root /usr/bin/env bash & source /env & aws s3 ls >> /test 2>&1
When I start the container I can see /env
was created with my variables but it never gets sourced.
source
? I'm pretty sure it should just besource
and not/source
– jesse_b Oct 14 '17 at 13:44env
and not using the actualenv
command? – jesse_b Oct 14 '17 at 13:47/env
that has the environment variables that need to be sourced. Is that wrong Philip? – terdon Oct 14 '17 at 13:49printenv > /env
and if it already exists he's overwriting it. I don't see much of an issue with creating such file but the fact that it is being created in the root directory makes it seem like an oversight. – jesse_b Oct 14 '17 at 13:50env
. The command is being used to findbash
in the current$PATH
and the file/env
has the variables that need to be sourced. – terdon Oct 14 '17 at 13:52/env
saves space. – Philip Kirkbride Oct 14 '17 at 13:52/
? – jesse_b Oct 14 '17 at 13:53/some/random/path/here/env
for all we know, this is just an example. – terdon Oct 14 '17 at 13:55cat $file | /grep 'string'
it's not unreasonable to ask if a slash in front of a well known command name is a mistake. Especially since the slash in front ofsource
was a mistake too. – jesse_b Oct 14 '17 at 13:56/
so there is no issue to be taken with files being saved in/
, because no files are being saved in/
. Using/env
in the question saves space since it avoids needing to write out a long path. We have no idea where the files actually are and that's not really relevant to the main issue of the question anyway. – terdon Oct 14 '17 at 13:58