3

I am having some problems trying to make a container that runs a cronjob. I can see cron running using top in the container but it doesn't write to the log file as the below example attempts to. The file stays empty.

I have read answers to the same question here:

But I could not make any of the suggestions work. For example I used the dockerfile from here: https://github.com/Ekito/docker-cron/

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

#Install Cron
RUN apt-get update
RUN apt-get -y install cron


# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

crontab:

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# Don't remove the empty line at the end of this file. It is required to run the cron job

It didn't work on my machine (windows 10). Apparently there seems to be a windows specific issue also reported by someone else: https://github.com/Ekito/docker-cron/issues/3

To test if it was just me doing something wrong I tried to do the same in a virtual machine running ubuntu (so an ubuntu host instead of my windows host) and that worked as expected. The log file is extended as expected.

So what can I do to try to make this work?

I tried writing to a mounted (bind) folder and making a volume to write to. Neither worked.

Jan Stanstrup
  • 1,152
  • 11
  • 28

2 Answers2

2

rferalli's answer on the github issue did the trick for me:

"Had the same issue. Fixed it by changing line ending of the crontab file from CRLF to LF. Hope this helps!"

NedRise
  • 66
  • 6
0

I have this problem too. My workaround is to use Task Scheduler to run a .bat file that start a container instead

Using Task Scheduler: https://active-directory-wp.com/docs/Usage/How_to_add_a_cron_job_on_Windows.html


hello.bat

docker run hello-world

TaskScheduler Action

cmd /c hello.bat >> hello.log 2>&1

Hope this help :)