我正在构建一个 docker 镜像来安排 Azure azcopy 作业。为了检查 cron 调度是否有效,我开始创建以下 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"]
crontab的内容是:
* * * * * root /bin/bash /root/scripts/azcopy_script.sh
azcopy_script.sh的内容是:
#!/bin/bash
echo "This is a test line printed on: $(date)" >> "/root/test_output.txt"
每分钟都应在
"/root/test_output.txt"
后附加一个新行,但不会创建文件。如果我在容器中启动交互式 shell 会话并手动运行文件,它工作正常。
cron 作业也列在容器中:
root@4bc9565fd3ff:~# crontab -l
* * * * * root /usr/bin/bash /root/scripts/azcopy_script.sh
如果我通过运行
* * * * * root /usr/bin/bash /root/scripts/azcopy_script.sh
将行 crontab -e
添加到根用户 crontab,作业会运行。
(将
/usr/bin/bash
更改为/bin/bash
,问候)