简体   繁体   English

cron.d中的Crons无法在Docker Container中运行的Ubuntu上运行

[英]Crons in cron.d won't run on Ubuntu running inside Docker Container

I have written a very small program consisting mainly of Scrapy scrapers. 我写了一个很小的程序,主要由Scrapy刮板组成。 I have it packaged in a docker container and need the scrapers to be called by cron. 我将其包装在docker容器中,需要cron调用刮板。

My docker-compose file is: 我的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

My Scraper Dockerfile is: 我的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

My crontab is (with a trailing new line): 我的crontab是(带有尾随新行):

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

This works perfectly well when running locally on my Mac running Sierra but when I put in on Amazon EC2 instance running the Amazon Linux AMI the crons do not get called. 当在运行Sierra的Mac上本地运行时,此方法效果很好,但是当我在运行Amazon Linux AMI的Amazon EC2实例中放置时,不会调用cron。 I used Filezilla to transfer the files from my Mac to my Amazon EC2 instance. 我使用Filezilla将文件从Mac传输到我的Amazon EC2实例。

AWS EC2: AWS EC2:

Docker version 1.12.6, build 7392c3b/1.12.6 Docker版本1.12.6,内部版本7392c3b / 1.12.6

My MacBook: 我的MacBook:

Docker version 17.03.0-ce, build 60ccb22 Docker版本17.03.0-CE,内部版本60ccb22

If I add the line 如果我添加线

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

using crontab -e nothing happens either. 使用crontab -e也不会发生任何事情。 The file cron.log is empty. 文件cron.log为空。

UPDATE: 更新:

I installed rsyslog and then started it: 我安装了rsyslog,然后启动了它:

service rsyslog start

Now in /var/log/syslog 现在在/ var / log / syslog中

Mar 25 21:49:01 4406b0e05b9f CRON[464]: Cannot make/remove an entry for the specified session 3月25日21:49:01 4406b0e05b9f CRON [464]:无法为指定会话创建/删除条目

I finally found a solution thanks to https://github.com/sameersbn/docker-gitlab/issues/173 我终于找到了一个解决方案,这要感谢https://github.com/sameersbn/docker-gitlab/issues/173

I commented out the following line in /etc/pam.d/cron 我在/etc/pam.d/cron中注释了以下行

session    required     pam_loginuid.so

Just need to work out how to do this automatically on docker-compose up. 只需要弄清楚如何在docker-compose上自动执行此操作即可。

Try adding permissions to the Dockerfile, eg, 尝试向Dockerfile添加权限,例如,

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

and change the location of the crontab 并更改crontab的位置

ADD scrapercron /etc/cron.d

Then in your crontab... 然后在您的crontab中...

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

And to test, try outputting to that tmp file 要进行测试,请尝试输出到该tmp文件

CMD cron && tail -f /tmp/cron_output

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

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