1

Okay so firstly I read some posts on this topic. That is how I ended up with my solution. Still I don’t find my mistake. Also I am more of a beginner.

So this is my docker file:

FROM conda/miniconda3
WORKDIR /app

RUN apt-get update -y
RUN apt-get install cron -y
RUN apt-get install curl -y


RUN conda update -n base -c defaults conda

RUN conda install mamba -n base -c conda-forge

COPY ./environment.yml ./environment.yml
RUN mamba env create -f environment.yml

# Make RUN commands use the new environment:
SHELL ["conda", "run", "--no-capture-output", "-n", "d2", "/bin/bash", "-c"]

#Setup cron
COPY ./cronjob /etc/cron.d/cronjob
RUN crontab /etc/cron.d/cronjob
RUN chmod 0600 /etc/cron.d/cronjob
RUN touch ./cron.log

COPY ./ ./

RUN ["chmod", "+x", "run.sh"]

ENTRYPOINT ["sh", "run.sh"]
CMD ["cron", "-f"]

What I want to do:

  1. Run my run.sh (I managed to do that.)
  2. Setup a cronjob inside my container which is defined in a file called cronjob (see content below)

My cronjob is not working. Why? Note that cron.log is empty. It is never triggered.

Also the output of crontab -l (run inside of the container) is:

$ crontab -l
# Updates every 15 minutes.
*/15 * * * * /bin/sh /app/cron.sh >> /app/cron.log 2&>1

cronjob

# Updates every 15 minutes.
*/15 * * * * /bin/sh /app/cron.sh >> /app/cron.log 2&>1

5
  • 1
    First of all, your cronjob command is wrong. You should have 2>&1 instead of 2&>1. Second. run ls -lh /app/cron.sh to see if your file is copied. Also be sure cron.sh is in the directory where your Dockerfile is.
    – Saeed
    Jan 25, 2022 at 10:33
  • A Docker container only runs one command; if you specify both ENTRYPOINT and CMD, they are combined together, so the single command your container runs is sh run.sh cron -f. It's usually easier to run only one command in a container, and the typical best practice here is to run a separate cron container. (Consider making run.sh the CMD, not the ENTRYPOINT, which will be easier to override with docker run options.)
    – David Maze
    Jan 25, 2022 at 11:54
  • @Saeed 1. Good point. I will fix that. 2. It was copied. 3. I confirm it is in the same dir. That was the fix. Thanks.
    – b3z
    Jan 25, 2022 at 12:18
  • @DavidMaze I don’t want to override I guess. But you would call cron -f inside of run.sh?
    – b3z
    Jan 25, 2022 at 12:20
  • 1
    I'd run a separate container. docker run ... your-image cron -f or using a Compose command: is pretty straightforward.
    – David Maze
    Jan 25, 2022 at 12:54

4 Answers 4

2

I had a similar issue with the crontab not being read

I was also using something like:

COPY ./cronjob /etc/cron.d/cronjob

Locally the cronjob file had permissions of 664 instead of 644. This was causing cron to log Sep 29 16:21:01 0f2c2e0ddbfd cron[389]: (*system*crontab) INSECURE MODE (group/other writable) (/etc/cron.d/crontab) (I actually had to install syslog-ng to see this happen).

Turns out cron will refuse to read cron configurations if they are writeable by others. I guess it makes sense in hindsight but I was completely oblivious to this.

Changing my cronjob file permissions to 644 fixed this for me (I did this on my local filesystem, the Dockerfile copies permissions over)

1

As Saeed said in this comment

First of all, your cronjob command is wrong. You should have 2>&1 instead of 2&>1. Second. run ls -lh /app/cron.sh to see if your file is copied. Also be sure cron.sh is in the directory where your Dockerfile is.

2&>1 was the mistake that I had made.

1

only you need to root right then it will solve issuse

*/15 * * * * root /bin/sh /app/cron.sh >> /app/cron.log 2&>1

0

As Saeed pointed out already, there is reason to believe you did not place your cron.sh script inside the container.

On top of that cron is programmed such that it does not log failed invocations anywhere. You can try to turn on some debug logging (I almost had to search cron's source to find the right settings years ago). Finally cron will send it's debug output to syslog - but in your container only cron is running, so the log entries are probably lost on that stage again.

That ultimately means you are in the dark and need to find the needle. But installing the script is a first good attempt.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.