1

I am building a docker image to schedule Azure azcopy jobs. To check whether the cron scheduling works I began with creating the following Dockerfile:

FROM ubuntu:20.04

# Updating packages and installing cron, wget and curl
RUN apt-get update && apt-get -y upgrade \
    && apt-get install -y cron \
    && apt-get install -y wget \
    && apt-get -y install curl

# Create directories in the home of the root user
RUN mkdir -p /root/Downloads && mkdir -p /root/bin && mkdir /root/scripts

WORKDIR /root/Downloads

# Download and extract azcopy binary 
RUN wget -O azcopy_v10.tar.gz https://aka.ms/downloadazcopy-v10-linux \
    && tar -xf azcopy_v10.tar.gz --strip-components=1

# Copy the azcpoy binary to the root user bin directory    
RUN cp azcopy /root/bin

# Add the root user bin directory to the shell path
ENV PATH="$PATH:/root/bin"

# Change working directory to the location were scripts are stored
WORKDIR /root/scripts

# Copy the azcopy script to the root user script directory and give proper permissions
COPY ./scripts/azcopy_script.sh .
RUN chmod 777 azcopy_script.sh

# Copy the crontab to the cron.d directory, give appropiate permissions and run it
COPY crontab /etc/cron.d/azcopy_cron_file
RUN chmod 777 /etc/cron.d/azcopy_cron_file && crontab /etc/cron.d/azcopy_cron_file && service cron restart

# Create entrypoint for cron
ENTRYPOINT ["cron", "-f"]

The content of crontab is:

* * * * * root /bin/bash /root/scripts/azcopy_script.sh

The content of azcopy_script.sh is:

#!/bin/bash
echo "This is a test line printed on: $(date)"  >> "/root/test_output.txt"

Every minute a new line should append to "/root/test_output.txt" but the file is not created. If I start an interactive shell session in the container and run the file manually, it works fine.

The cron job is also listed in the container:

root@4bc9565fd3ff:~# crontab -l
* * * * * root /usr/bin/bash /root/scripts/azcopy_script.sh

If I add line * * * * * root /usr/bin/bash /root/scripts/azcopy_script.sh to the root user crontab by running crontab -e, the job does run.

Bakkie103
  • 47
  • 6
  • Is in your container a file `/root/scripts/azcopy_script.sh`? – Cyrus Feb 28 '23 at 18:31
  • @Cyrus, yes there is. – Bakkie103 Feb 28 '23 at 18:57
  • `root@87854cececf8:~# file /root/scripts/azcopy_script.sh /root/scripts/azcopy_script.sh: Bourne-Again shell script, ASCII text executable` – Bakkie103 Feb 28 '23 at 19:24
  • I deleted my answer as it doesn't solve the issue and may deter others from bothering to answer. Good luck. – Mark Setchell Feb 28 '23 at 21:41
  • Whatever you are hoping to accomplish, **`chmod 777` is *wrong* and *dangerous.*** You absolutely do not want to grant write access to executable or system files to all users under any circumstances. You will want to revert to sane permissions ASAP (for your use case, probably `chmod 755`) and learn about the Unix permissions model before you try to use it again. The Docker container hopefully shields you from the worst risks associated with this antipattern, but that doesn't change the fact that you should basically never use it. – tripleee Mar 01 '23 at 06:07
  • 1
    The post [Cron job not running](https://stackoverflow.com/questions/22743548/cronjob-not-running) has some general `cron` troubleshooting tips. – tripleee Mar 01 '23 at 06:21
  • 1
    @Bakkie103 -> updated the answer , ( still laughing the "playing political correctness" comments that tell you "never use 777" in a single-user environment .. :D ) – Bash Stack Mar 03 '23 at 17:56

1 Answers1

2

You might suffer from 2 Problems:

Crontab Problems:

  • Cron does not use your file as the system crontab file is kept in /etc/crontab, so adjust your Dockerfile

    (current): COPY crontab /etc/cron.d/azcopy_cron_file

    (proper ): COPY crontab /etc/crontab

  • inline alternative with root user crontab:

    normally cron does advise you to use crontab -e,

    you can do it inline e.g. with a custom entrypoint script:

    (crontab -l 2>/dev/null; echo "* * * * * /bin/bash /root/scripts/azcopy_script.sh")| crontab -

Path problems (?)

Your crontab -l shows a wrong path /usr/bin/bash .. ( but your first line is right )

  • change /usr/bin/bash to /bin/bash , ( or softlink ln -s /bin/bash /usr/bin/bash )
* * * * * root /bin/bash /root/scripts/azcopy_script.sh

standard ubuntu builds do not have the file /usr/bin/bash thats why your cron will not be able to find it


Note:

you might also set your path and shell in crontab since not all paths are used by cron

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

when using the above example in your crontab and the bash file set to executable permissions ,you might just use the following line * * * * * root /root/scripts/azcopy_script.sh

regards

Bash Stack
  • 509
  • 3
  • 12