Reputation: 718
Im using the docker Mongo container and am attempting to take a backup via a bash script. The script, executed on its own, works fine. I can also see in syslog that cron is running but the command is not showing up. If I open my file with crontab -e, then save and quit (:wq), then restart cron, the job runs fine.
Here is the relevant section of my dockerfile:
ADD mongocron /etc/cron.d/
RUN tr -d '\015' < /etc/cron.d/mongocron > /etc/cron.d/mongocron
#RUN touch /etc/cron.d/mongocron
#RUN echo "* * * * * /db_scripts/MongoDBBackup.sh >> /db_scripts/logs/backup.log\n" > /etc/cron.d/mongocron
RUN crontab /etc/cron.d/mongocron
RUN chmod 0644 /etc/cron.d/mongocron
This is what is in the file mongocron:
* * * * * /db_scripts/MongoDBBackup.sh >> /db_scripts/logs/backup.log
This is the syslog output before resaving:
Restarting cron on its own does not fix it. I have a feeling it has something to do with line endings, so thats why you see the commented out "echo" strategy in the dockerfile with a newline. I have also verified (before saving) that my command does show in when I crontab -l
Upvotes: 3
Views: 1158
Reputation: 401
I've faced this issue too and a missing newline in the end of file was the reason. @opHASnoNAME 's answer is right but when it starts working only after editing, new line is the reason. Your editor just adds it by default.
Upvotes: 2
Reputation: 20726
I was struggling with the same problem in the past.
Here is a working example, tested in the wild, taken from our DevBlog.
FROM ubuntu:latest
ADD start.sh /bin/start.sh
RUN chmod +x /bin/start.sh
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/thecron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/thecron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# start script
CMD /bin/bash /bin/start.sh
The start.sh, think you don't need the part with export of env variables..
# export all environment variables to use in cron
env | sed 's/^\(.*\)$/export \1/g' > /root/envs.sh
chmod +x /root/envs.sh
# Run the command on container startup
cron && tail -f /var/log/cron.log
The Cron file, copied while building the image.
0 1 * * * root . /root/envs.sh;/bin/backup.sh >> /var/log/cron.log 2>&1
Upvotes: 0