Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

394
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda