繁体   English   中英

如何每分钟通过cron运行一次dockerized python脚本?

[英]How to run a dockerized python script via cron once every minute?

我希望每分钟运行一次 dockerized python 脚本,我想了解如何让这个脚本每分钟运行一次,我会通过 cron 来做吗?

它将通过 api 调用收集数据并加载到我设置的另一个 docker 容器中的 mysql 数据库,但现在作为示例,这里是我正在运行的:

Dockerfile:

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "./loading_data.py" ]

加载数据.py:

print('i am loading data to mysql...')

我已经构建了我的 docker python 图像:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker_python       latest              f7222d16bbce        3 minutes ago       938MB

如果我运行图像,它会在正确构建时执行打印语句吗? 所以我想:

  1. 将无限循环写入 python 脚本并让我的操作系统启动 docker 容器并在失败时重新启动它? (感觉不对)

  2. 我想要一个 cron 作业启动容器,销毁它并每分钟重新启动它吗? (感觉过分了)

  3. 或者有更简单更优雅更好的方法吗?

--------------------------------------------------试图:

我一直在尝试按照https://github.com/cheyer/docker-cron示例的建议将 python 脚本设置为 cron 作业,这里是我的构建:

Dockerfile:

 #python install----------
    FROM python:3.6 
    WORKDIR /usr/src/app
    COPY requirements.txt ./
    RUN pip install --no-cache-dir -r requirements.txt
    COPY . .
    CMD [ "python", "./loading_data.py" ]
    #cron install----------
    FROM ubuntu:latest
    # Install cron
    RUN apt-get update
    RUN apt-get install cron
    # Add crontab file in the cron directory
    ADD crontab /etc/cron.d/simple-cron
    # Add shell script and grant execution rights
    ADD script.sh /script.sh
    RUN chmod +x /script.sh
    # Give execution rights on the cron job
    RUN chmod 0644 /etc/cron.d/simple-cron
    # Create the log file to be able to run tail
    RUN touch /var/log/cron.log
    # Run the command on container startup
    CMD cron && tail -f /var/log/cron.log

脚本.sh:

python ./loading_data.py >> /var/log/cron.log 2>&1

定时任务:

* * * * * root /script.sh
# An empty line is required at the end of this file for a valid cron file.

构建并运行容器,cron作业每分钟运行都没有问题,但是在容器内找不到python。 我将如何修复这个问题? 我相信我非常接近:

sudo docker exec -i -t 3c54ffaf2674db1ab0751abe93ce77956d8b5b594a7c48cc589c4841761d4e71 /bin/bash 
    root@3c54ffaf2674:/# cat /var/log/cron.log
    /script.sh: 1: /script.sh: python: not found
    /script.sh: 1: /script.sh: python: not found
    /script.sh: 1: /script.sh: python: not found

正如您已经列出了解决方案及其优缺点。 我要做的一种方法是,编写一个玉米作业,该作业将每分钟运行脚本"python ./loading_data.py"并将其放入同一个容器中,这意味着容器始终在运行,并且它也将运行玉米作业里面。

暂无
暂无

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

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