【问题标题】:Python logging while crontab in docker containerdocker容器中的crontab时的Python日志记录
【发布时间】:2021-08-18 06:23:30
【问题描述】:

我正在运行一个登录到文件的 python 作业:

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', filename='/app/logs/ups_tracking.log')
self.logger = logging.getLogger('TRACK-UPS')

手动运行作业时,日志文件会很好地创建/增加新条目。

通过 crontab(如下语法)运行时,日志未按预期写入。

### TRACKING UPS ###
* * * * * python /app/UPS/parcels.py

root@91067d2217e7:/app/logs# service cron status
[ ok ] cron is running.

我在 docker 容器中运行整个东西,dockerfile 如下:

#Create the flask custom image
FROM python:latest
    
# Place your flask application on the server
COPY ./back /app/

WORKDIR /app

# Install requirements.txt
RUN /usr/local/bin/python -m pip install --upgrade pip 
RUN pip3 install --no-cache-dir -r requirements.txt

RUN apt-get update && apt-get install -y netcat cron

COPY ./config/init.sh /tmp/init.sh

RUN chmod +x /tmp/init.sh

# Copy crontab_file file to the cron.d directory
COPY ./config/crontab_file /etc/cron.d/crontab_file
 
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/crontab_file

# Apply cron job
RUN crontab /etc/cron.d/crontab_file

# Start CRON service
RUN service cron start

EXPOSE 8889
ENTRYPOINT ["/tmp/init.sh"]

我在这里错过了什么吗?

谢谢!

【问题讨论】:

  • service 在 Docker 中实际上并不适用。一个容器只运行一个进程,因此您可能需要在单独的容器中运行 cron 守护进程。
  • 很奇怪,因为如果我创建一个基本的 cron 条目(* * * * * echo « toto » > myfile),则会创建日志文件

标签: python linux docker cron


【解决方案1】:
# Start CRON service
RUN service cron start

表示 crond 仅在 RUN 阶段运行。

看到我想知道你是否也在 /tmp/init.sh 中启动 cron 吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 2022-09-29
    • 2023-03-23
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多