skip to Main Content

I’ve been trying to set cron up within a Docker container. It is now working fine. What I want now is a log file.

This is my crontab:

* * * * * python /code/data_etl.py > /proc/1/fd/1 2> /proc/1/fd/2

My Dockerfile CMD is CMD ["cron", "-f"].

I was only able to get this working by following the answer here
How to run a cron job inside a docker container?

I’m not 100% sure, but I believe the f flag is running cron in the foreground, rather than as a background process.

However, I’m not sure why this line > /proc/1/fd/1 2> /proc/1/fd/2 is really necessary, and therefore, don’t know how to amend it so that I can store a log file in my Docker container.

2

Answers


  1. Good Morning! You should be try to change your parameters in cron file to below:

    * * * * * python /code/data_etl.py > /proc/1/fd/1
    

    Only you need one of these, i think is unnecessary use 2> /proc/1/fd/2

    Let me know your result! Have a great day!

    Login or Signup to reply.
  2. > /proc/1/fd/1 redirects stdout of your python process (your task) to stdout of the process with PID 1 (which is the crod daemon). 2> /proc/1/fd/2 redirects stderr of your task to stderr of the cron daemon.

    Unless you do that, the output of your task won’t appear in docker logs. And this trick works provided you run your tasks as root.

    But doesn’t that seem kind of tricky? Why not use a docker-friendly cron implementation?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search