简体   繁体   English

Docker:cron作业未运行

[英]Docker: cron job not being run

I am trying to get a cronjob to run on a project deployed with docker. 我试图让cronjob在使用docker部署的项目上运行。 I am able to build the image and run the container successfully, however, when I log into the container and check /var/log the cron job has not run. 我能够构建映像并成功运行容器,但是,当我登录到容器并检查/var/log ,cron作业未运行。

Here are the files: 这些是文件:

Dockerfile Dockerfile

# use this image, as we'll need to run chron etc.
FROM phusion/baseimage:0.11

# Install python3, pip and cron
RUN apt-get update && \
    apt-get -y install cron python3 python3-pip && \
               pip3 install --upgrade pip

# Create required volumes
VOLUME /var/log   
VOLUME /srv/data

# Set environment
ENV TEST_ENV=/srv/data

COPY fetcher.py /fetcher.py

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/cron-fetcher

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cron-fetcher

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Apply cron job
RUN crontab /etc/cron.d/cron-fetcher


# Run the command on container startup
CMD ["cron", "-f"]

crontab crontab中

# placed in /etc/cron.d 
* * * * * root python3 /fetcher.py >> /var/log/fetcher.log

fetcher.py fetcher.py

#!/usr/bin/env python

import urllib.request

# python script which needs an environment variable and runs as a cron job
import datetime
import os

test_environ = os.environ["TEST_ENV"]
print ("Cron job has run at {0} with environment variable '{1}'".format(datetime.datetime.now(), test_environ))
host_path = test_environ
url = 'http://winterolympicsmedals.com/medals.csv'
response = urllib.request.urlopen(url)
html = response.read()
filename = "{0}/data.csv".format(host_path)

with open(filename, 'wb') as f:
    f.write(html)

Why is the cronjob not running? 为什么cronjob没有运行?

Files in /etc/cron.d need to specify the user: /etc/cron.d中的文件需要指定用户:

# placed in /etc/cron.d 
* * * * * root python3 /fetcher.py >> /var/log/fetcher.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM