1

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.

TheDataPanda
  • 187
  • 1
  • 6

2 Answers2

1

> /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?

x-yuri
  • 16,722
  • 15
  • 114
  • 161
0

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!

euTIMER
  • 681
  • 3
  • 12
  • Hi. Not sure that's what I'm after. I'm really just looking for a way to store cron output to a file, whilst not breaking what I already have in place. – TheDataPanda Jul 26 '22 at 10:55
  • Oh okey, sorry for that. I understand other thing. Well i edit my answer and try it. – euTIMER Jul 26 '22 at 11:00
  • Unless you specify `> /proc/1/fd/1`, you won't see `stdout` output of your task in `docker logs`. Unless you specify `2> /proc/1/fd/2`, you won't see `stderr` output of your task in `docker logs`. More on it in [my answer](https://stackoverflow.com/a/75354118/52499). – x-yuri Feb 05 '23 at 17:24