Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

389
Views
Cron job doesn't work until I re-save cron file in docker container

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: enter image description here

And here is a picture after: After 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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!