【问题标题】:Crontab in alpine image [duplicate]高山图像中的 Crontab [重复]
【发布时间】:2021-10-15 07:39:23
【问题描述】:

我想在 alpine 映像中创建一个 cronjob,它将每 15 秒运行一次。 dockerfile 中的命令是

RUN echo "*/15 * * * * * /etc/init.d/aws-env-setup.sh" >> /var/spool/cron/crontabs/root

下面是图片启动后的文件。

# do daily/weekly/monthly maintenance
# min   hour    day month   weekday command
*/15    *   *   *   *   run-parts /etc/periodic/15min
0   *   *   *   *   run-parts /etc/periodic/hourly
0   2   *   *   *   run-parts /etc/periodic/daily
0   3   *   *   6   run-parts /etc/periodic/weekly
0   5   1   *   *   run-parts /etc/periodic/monthly
*/15 * * * * * /etc/init.d/aws-env-setup.sh

cronjob 没有运行。

如何在 alpine 容器中成功创建 cronjob。我的目标是在容器启动后 15 秒只运行一次 cronjob。 我想在 cron 下运行的 sh 文件的最后一行是 - crontab -r

【问题讨论】:

标签: docker cron alpine


【解决方案1】:

我对 cron 很恼火。它是如此标准,如此普遍,应该如此直截了当,但现实是,它往往只是一团糟。我自己说screw cron,基于这个helper让自己成为一个python服务

def cron(delay, task):

    # delay in seconds
    next_time = time.time() + delay

    while True:
      try:
          task()
      except Exception:
          traceback.print_exc()
          print(str(traceback.print_exc()))

      next_time += (time.time() - next_time) // delay * delay + delay
      time.sleep(max(0, next_time - time.time()))

将延迟设置为 15,你的任务是

cmd = "/etc/init.d/aws-env-setup.sh"
task = subprocess.call(cmd, shell=True)

并从中提供服务。你可以使用你的 linux 来处理它,但是由于你已经在 docker 内部工作了,你可以设置一个 docker restart 策略,让 docker 把它当作守护进程来处理。

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2022-10-13
  • 2017-12-20
相关资源
最近更新 更多