【问题标题】:Crons in cron.d won't run on Ubuntu running inside Docker Containercron.d 中的 Cron 无法在 Docker 容器内运行的 Ubuntu 上运行
【发布时间】:2017-08-18 17:41:57
【问题描述】:

我编写了一个非常小的程序,主要由 Scrapy 刮板组成。我把它打包在一个 docker 容器中,需要 cron 调用刮板。

我的 docker-compose 文件是:

version: '2'
services:
  admin-panel:
    env_file: ./Admin-Panel/.env
    build: ./Admin-Panel/
    volumes:
      - ./Admin-Panel/app:/code/app
      - ./Admin-Panel/flaskadmin.py:/code/flaskadmin.py
    ports:
      - "5000:5000"
  scraper:
    env_file: ./Admin-Panel/.env
    build: ./Scraper/
    volumes:
      - ./Scraper/spiders:/spiders

我的 Scraper Dockerfile 是:

FROM ubuntu:latest
ENV TERM xterm

RUN apt-get update
RUN apt-get install -y python3-pip python3.5-dev build-essential  
RUN apt-get install -y libssl-dev nano cron libpq-dev libffi-dev curl

ADD ./requirements /requirements
ADD crontab /etc/cron.d/scrapers

RUN pip3 install --upgrade pip
RUN pip3 install -r /requirements/base.txt

RUN touch /var/log/cron.log

CMD cron && tail -f /var/log/cron.log

我的 crontab 是(带有尾随的新行):

* * * * * root /usr/local/bin/scrapy runspider /spiders/myspider.py
* * * * * root /bin/date >> /tmp/cron_output

这在我运行 Sierra 的 Mac 上本地运行时效果很好,但是当我在运行 Amazon Linux AMI 的 Amazon EC2 实例上运行时,不会调用 crons。我使用 Filezilla 将文件从我的 Mac 传输到我的 Amazon EC2 实例。

AWS EC2:

Docker 版本 1.12.6,构建 7392c3b/1.12.6

我的 MacBook:

Docker 版本 17.03.0-ce,构建 60ccb22

如果我添加行

* * * * * root /bin/date >> /tmp/cron_output

使用 crontab -e 也没有任何反应。文件 cron.log 为空。

更新:

我安装了 rsyslog 然后启动它:

service rsyslog start

现在在 /var/log/syslog 中

3 月 25 日 21:49:01 4406b0e05b9f CRON[464]:无法为指定会话创建/删除条目

【问题讨论】:

  • 一个 docker 容器不能运行一整套服务。 crond 很可能根本没有在您的容器中运行,除非您已明确配置它这样做。

标签: ubuntu docker amazon-ec2 cron ubuntu-16.04


【解决方案1】:

感谢https://github.com/sameersbn/docker-gitlab/issues/173,我终于找到了解决方案

我在 /etc/pam.d/cron 中注释掉了以下行

session    required     pam_loginuid.so

只需要弄清楚如何在 docker-compose up 上自动执行此操作。

【讨论】:

  • 确实有效,知道如何用 docker 来做吗?
【解决方案2】:

尝试向 Dockerfile 添加权限,例如,

RUN chmod 0744 /spiders/myspider.py /etc/cron.d/scrapercron

并更改 crontab 的位置

ADD scrapercron /etc/cron.d

然后在你的 crontab 中...

HOME=/spiders * * * * * root /spiders/myspider.py >> /tmp/cron_output 2>&1

为了测试,尝试输出到那个 tmp 文件

CMD cron && tail -f /tmp/cron_output

【讨论】:

    猜你喜欢
    • 2019-02-17
    • 2015-12-28
    • 2018-08-29
    • 2018-02-27
    • 1970-01-01
    • 2019-10-13
    • 2021-03-04
    • 1970-01-01
    • 2018-12-27
    相关资源
    最近更新 更多