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.