我写了一个非常小的程序,主要由 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 version 1.12.6, build 7392c3b/1.12.6
我的MacBook:
Docker version 17.03.0-ce, build 60ccb22
如果我添加该行
* * * * * root /bin/date >> /tmp/cron_output
使用 crontab -e 也没有任何 react 。文件 cron.log 为空。
更新:
我安装了 rsyslog 然后启动它:
service rsyslog start
现在在/var/log/syslog
Mar 25 21:49:01 4406b0e05b9f CRON[464]: Cannot make/remove an entry for the specified session
最佳答案
感谢https://github.com/sameersbn/docker-gitlab/issues/173,我终于找到了解决方案
我在/etc/pam.d/cron 中注释掉了以下行
session required pam_loginuid.so
只需要弄清楚如何在 docker-compose up 上自动执行此操作。
关于ubuntu - cron.d 中的 Cron 无法在 Docker 容器内运行的 Ubuntu 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022296/