Issue
Putting together solutions from various previous answers to the topic, and I am still failing. Here is my Dockerfile:
FROM python:3.7
RUN apt-get update &&\
apt-get install -y cron &&\
apt-get clean
# do this now to use layer cache mechanism
COPY requirements.txt /
RUN pip install -r /requirements.txt
RUN mkdir -p /usr/local/src/app
WORKDIR /usr/local/src/app
ADD script .
ADD StockLogger.py .
ADD crontab.txt .
COPY entry.sh .
RUN chmod 755 script entry.sh && touch /var/log/cron.log
CMD ./entry.sh && tail -f /var/log/cron.log
Here is the entry.sh
#!/bin/sh
set -e
# start cron
service cron start
cp ./crontab.txt /etc/cron.d/
#fix link-count, as cron is being a pain, and docker is making hardlink count >0 (very high)
touch /etc/crontab /etc/cron.*/*
echo Added crontab
whoami
echo ***entry.sh done***
Here is crontab.txt
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# Keeping the empty next line
When I run the container with docker run sprevrha/crontester:latest
, the tail -f
in the CMD
keeps it nicely running, but doesn't produce any output. The file is also empty when I exec into the running container and inspect it with cat
. Also, crontab -l
says there are no cron jobs for root. The /etc/cron.d/crontab.txt
is there, and it is owned by root.
root@16d01a1a2c0c:/usr/local/src/app# ls -l /etc/cron.d
total 4
-rwxr-xr-x 1 root root 1020 Jan 7 21:20 crontab.txt
Link count is 1, so presumably ok.
What am I doing wrong?
Solution
Only from reading a little bit documentation, it does not support any dots and suffixes like .txt and its path is directly under /etc/
From documentation: /etc/cron.d/ Put all scripts here and call them from /etc/crontab file.
Answered By - Matus Danoczi