【问题标题】:Cron (and systemd) in centos7 in dockerdocker 中 centos7 中的 Cron(和 systemd)
【发布时间】:2017-09-21 14:20:53
【问题描述】:

我想在 docker 中运行的 centos7 操作系统中运行 cron。当我尝试启动 crond 时,我得到:

Failed to get D-Bus connection: Operation not permitted

谷歌搜索显示这是因为 systemd 没有运行。但是当我尝试开始时,我得到:

bash-4.2# /usr/lib/systemd/systemd --system --unit=basic.target
systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
Detected architecture x86-64.
Set hostname to <7232ef24bdc8>.
Initializing machine ID from random generator.
Failed to install release agent, ignoring: No such file or directory
Failed to create root cgroup hierarchy: Read-only file system
Failed to allocate manager object: Read-only file system

有人知道我如何在这里运行 crond 吗?

【问题讨论】:

  • 在容器中运行/usr/sbin/cron -f,而不是通过systemd
  • 这给了我No such file or directory。我确实安装了cronie 软件包。
  • 你用的是哪个centos镜像?
  • FROM centos:centos7
  • 如果您将其作为主 CMD 运行,则需要安装 crontabs 并运行 crond -n,如果要将其作为后台进程运行,则需要安装 crond

标签: docker cron centos7 systemd


【解决方案1】:

我快速检查了这是否可以与docker-systemctl-replacement script 一起使用。该脚本所做的是读取 *.service 文件(无需 systemd 守护程序的帮助),以便知道如何启动和停止服务。

在“yum install -y cronie”之后,我能够“systemctl.py start crond”,在这种情况下,我可以看到一个正在运行的进程“/usr/sbin/crond -n”。可以将 systemctl.py 安装为默认 CMD,这样它也可以从保存的图像中简单地启动和停止容器。

【讨论】:

    【解决方案2】:

    您可以通过这种方式在docker内部运行cron服务:

    在容器内映射 etc/cron.d/crontab 文件,crontab 文件应该包含您的 cronjobs,请参阅下面的 cronjob 示例:

    @reboot your-commands-here >> /var/log/cron.log 2>&1
    @reboot sleep 02 && your-commands-here >> /var/log/cron.log 2>&1
    0 * * * * your-commands-here >> /var/log/cron.log 2>&1
    

    在您的 Dockerfile 中:

    chmod -R 0664 /etc/cron.d/*
    # Create the log file to be able to run tail and initiate my crontab file
    RUN touch /var/log/cron.log && crontab /etc/cron.d/crontab
    
    # Run the command on container startup
    CMD /etc/init.d/cron restart && tail -f /var/log/cron.log
    
    # Run the command on container startup
    CMD /etc/init.d/cron restart && tail -f /var/log/cron.log
    
    #start supervisor 
    ENTRYPOINT ["service", "supervisor", "start"]
    

    要在主管下运行 cron,请使用以下配置:

    [program:cron]
    command=    cron -f
    startsecs    = 3
    stopwaitsecs = 3
    autostart    = true
    autorestart = true
    

    【讨论】:

    • 我应该运行 cron 还是 crond?
    • 如果你正在运行 centos,请使用 crond
    • 当我尝试与主管一起启动它时,它不会熬夜。但是,如果我手动启动它,它确实会保持不变。主管启动可能有什么问题?
    • 主管需要在无守护模式下启动。你想要在你的 supervisord.conf [supervisord] nodaemon=true
    • 我在无守护模式下运行它。
    猜你喜欢
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2016-06-09
    • 2018-10-09
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多