简体   繁体   English

在Docker容器中运行预定的cron作业的解决方案?

[英]Solution for running scheduled cron job within a Docker container?

I have a Docker container in which I have my Python tools installed, including my Luigi pipeline interface. 我有一个Docker容器,我安装了我的Python工具,包括我的Luigi管道接口。 I would like to run a shell script which kicks off my Luigi pipeline on a weekly basis using cron. 我想运行一个shell脚本,它使用cron每周启动我的Luigi管道。

I have tried high and low to get cron to work within a Docker container. 我尝试过高低,让cron在Docker容器中工作。 I cannot, for the life of me, get my crontab -e file to run. 在我的生活中,我不能让我的crontab -e文件运行。

In my file I have: 在我的档案中,我有:

0 0 * * Sun /data/myscript.sh

followed by a new line. 接着是一条新线。 Cron is running in the background - ps aux | grep cron Cron正在后台运行 - ps aux | grep cron ps aux | grep cron shows /usr/sbin/cron is running. ps aux | grep cron显示/usr/sbin/cron正在运行。 Furthermore, in my /var/log/syslog file, I have: 此外,在我的/var/log/syslog文件中,我有:

/USR/SBIN/CRON[2037]: (root) CMD (/data/myscript.sh)

I've also tried using 0 0 * * Sun . /root/.bashrc ; sh /data/myscript.sh 我也试过使用0 0 * * Sun . /root/.bashrc ; sh /data/myscript.sh 0 0 * * Sun . /root/.bashrc ; sh /data/myscript.sh

However, my script does not run (when I run my script manually using bash myscript.sh , I get the expected results). 但是,我的脚本没有运行(当我使用bash myscript.sh手动运行我的脚本时,我得到了预期的结果)。

Suggestions? 建议?

Scheduled tasks won't run inside of a normal container since there is no scheduler running. 由于没有调度程序正在运行,因此计划任务不会在普通容器内运行。 The only active task will be that which you have elected to run via the CMD keyword or the Entrypoint. 唯一的活动任务是您选择通过CMD关键字或Entrypoint运行的任务。

In order to execute schedule tasks, it's more prudent to utilize the host scheduler and docker exec commands: 为了执行计划任务,使用主机调度程序和docker exec命令更为谨慎:

docker exec <container> <command>
docker exec <container> /data/myscript.sh

So you would end up with a cron on your host something like : 所以你最终会在你的主机上找到一个cron:

(Crontab Style) 0 * * * * root docker exec mycontainer /data/myscript.sh (Crontab样式)0 * * * * root docker exec mycontainer /data/myscript.sh

If you have a cluster, you would have to query the cluster first to locate the container, or even have a script do it for you. 如果您有一个群集,则必须首先查询群集以找到容器,或者甚至让脚本为您执行此操作。

A container is meant to only one run main process. 容器仅用于一个运行主进程。 You either need to run crond as the main process for a container, or ensure that crond is running alongside your main process. 您需要运行crond作为容器的主要进程,或者确保crond与主进程一起运行。 This kind of breaks the contracts / point of containers, but sometimes it's easier to set it up this way. 这种打破容器的合同/点,但有时这样设置起来更容易。 Instructions below: 说明如下:

My Dockerfile has the following ENTYPOINT : 我的Dockerfile具有以下ENTYPOINT

ENTRYPOINT ["/startup.sh"]

And then within startup.sh I do a couple of things to spin up the container, but most importantly before executing the last command, I have this: 然后在startup.sh我做了一些事情来启动容器,但最重要的是在执行最后一个命令之前,我有这个:

crond
exec start_my_service

crond starts the daemon that executes the crons, and start_my_service then becomes the primary process for my container. crond启动执行crons的守护进程,然后start_my_service成为我的容器的主要进程。

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

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