【发布时间】:2015-12-13 08:30:51
【问题描述】:
人们通常如何使用 docker 处理 cron 作业?我见过的最常见的情况是会有一个仅运行 crond 和代码库的 sidekick 映像,但是当使用 cronie 时,我无法读取在 docker 命令行上传入的任何环境变量。
具体来说,我会这样做:
docker run -d --name cron -e VAR1=val1 -e VAR2=val2 cron_image start
在图片里面我们会有这个:
[root@dae7207bf10e /]# yum info cronie
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.ash.fastserv.com
* epel: mirror.cs.pitt.edu
* extras: mirror.vcu.edu
* updates: mirror.us.leaseweb.net
Installed Packages
Name : cronie
Arch : x86_64
Version : 1.4.11
Release : 13.el7
Size : 211 k
Repo : installed
Summary : Cron daemon for executing programs at set times
URL : https://fedorahosted.org/cronie
License : MIT and BSD and ISC and GPLv2+
Description : Cronie contains the standard UNIX daemon crond that runs specified programs at
: scheduled times and related tools. It is a fork of the original vixie-cron and
: has security and configuration enhancements like the ability to use pam and
: SELinux.
[root@dae7207bf10e /]# cat /usr/local/bin/start
#!/bin/bash
/usr/bin/env > /var/tmp/docker_env
/usr/sbin/crond -n
我的 crontab 将如下所示:
SHELL=/bin/bash
5 16 * * * source /var/tmp/docker_env; /usr/local/bin/randomchallenge &> /var/log/randomchallenge.log
最初我根本没有源代码并尝试直接使用变量,但是看起来 cronie 并没有将它们呈现给调用的作业(这在绝大多数用例中确实有意义)。我试过以各种方式拉入这个 env 文件,但没有运气,我的程序永远无法读取变量。即使将整个东西包装在一个引入 env 的 shell 脚本中也无法完成这项工作。
人们如何处理这种事情?硬编码值不是一种选择。我想我可以让启动脚本即时生成 crontab,但这看起来真的很难看。
【问题讨论】: