简体   繁体   English

带有 docker-compose.yaml 的动态 crontab

[英]dynamic crontab with docker-compose.yaml

I'm trying to build a dynamic cron job using as basis this link: How to run a cron job inside a docker container When I'm the container $var1 prints the right value, but seens like cron doesnt like env variables.... Any way to go around this?我正在尝试使用此链接作为基础构建动态 cron 作业: How to run a cron job inside a docker container当我是容器时 $var1 打印正确的值,但看起来像 cron 不喜欢 env 变量... . 有什么办法解决这个问题吗? Dockerfile: Dockerfile:

FROM alpine:latest
RUN apk update
RUN apk add rsync run-parts curl
COPY /Scripts/* /
RUN chmod 755 /entry.sh /script.sh /start.sh
#RUN /usr/bin/crontab /crontab.txt
CMD ["/entry.sh"]

entry.sh:入口.sh:

#!/bin/sh
# start cron
sh start.sh
/usr/sbin/crond -f -l 8

start.sh:开始.sh:

#!/bin/bash

/usr/bin/crontab /crontab.txt

docker-compose.yml: docker-compose.yml:

version: '3.6'
services:
    test:
        image: test1
        environment:
            - var1=${var1_env}
    

.env: .env:

var1_env=*/1

crontab.txt: crontab.txt:

$var1 * * * * /script.sh >> /var/log/script.log

Cronjobs traditionally run in a fresh, clean environment. Cronjobs 传统上在一个新鲜、干净的环境中运行。 The behavior you're seeing is that the cron daemon is 'cleaning up' the environment before running your job.您看到的行为是 cron 守护进程在运行您的作业之前“清理”环境。

You have two options:您有两个选择:

  1. Have the cronjob run a shell script.让 cronjob 运行一个 shell 脚本。 That shell script sets up the environment, then runs the command.该 shell 脚本设置环境,然后运行命令。 https://stackoverflow.com/a/2229861/530160 https://stackoverflow.com/a/2229861/530160
  2. Define the variables inside the crontab.在 crontab 中定义变量。 https://stackoverflow.com/a/10657111/530160 https://stackoverflow.com/a/10657111/530160

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

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