2

Relevant parts of Dockerfile:

RUN apt-get install -y cron
RUN service cron start

ADD cronjob /etc/cron.d/gptswmm-cron
RUN chmod 0644 /etc/cron.d/gptswmm-cron
RUN touch /var/log/cron.log

RUN crontab /etc/cron.d/gptswmm-cron
RUN cron

I check ps -ef output and cron isn't there. Whatever, I can spin it up manually after the fact with cron command and it shows up (just to check all my boxes, also do service cron start).

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 15:54 pts/0    00:00:00 /bin/bash
root        41     0  0 15:59 ?        00:00:00 cron
root        65     0  0 16:02 ?        00:00:00 ps -ef

I do crontab -l and get the same as in my cronfile (which does have the empty line and the end too):

MAILTO=""
* * * * * root python /var/test/testcron.py >> /var/log/cron.log 2>&1

Python file simply creates (or appends to, if existing) a test file in the same directory (ensured same dir as script location), repeating the same word. As simple a test as you can get. (I originally had it echo-ing to log file, but did this as I'm more comfortable with what's going on in a python script than bash). Python file is owned by root with all permissions to owner.

Yet when I check where the text file should be, nothing. When I check /var/log/cron.log, it's empty.

When I manually call python /var/test/testcron.py it works and creates the output file.

So I get some system logging going, redoing the Dockerfile with this at the end:

RUN apt-get install -y rsyslog

Rebuild and spin up container. Start rsyslog first rsyslogd, then cron with cron and double-checking with service start cron.

Check /var/log/syslog and cron seems to be getting called, these basic two lines repeat every minute:

... CRON[48]: (root) CMD (python /var/test/testcron.py >> /var/log/cron.log 2>&1^M)
... CRON[47]: (root) CMD (root python /var/test/testcron.py >> /var/log/cron.log 2>&1^M)

I'm at a loss here. Been googling and searching for various solutions, but nothing so far has worked.

3
  • 1
    Try using absolute path to python binary. Also remove this part (2>&1^M) and maybe some errors will show up. Btw what is that "^M"? Tough to know what's going wrong here. Commented Sep 12, 2018 at 17:10
  • Ran it with python pointed at /usr/bin/python and w/o the 2>&1 stuff. Not sure which did it (I'll test and figure out, but my guess is the 2nd thing). The ^M still gets appended at the end of the syslog, just looks like .../cron.log^M) now. However, I'm seeing the output file now. I'll report back shortly whether pointing it to python absolutely or the 2>&1 removal was what did it. Commented Sep 12, 2018 at 17:26
  • Possible duplicate of How to run a cron job inside a docker container?
    – David Maze
    Commented Sep 12, 2018 at 23:32

1 Answer 1

1

Looks like I had remove the 2>&1 from the cron job:

* * * * * root python /var/test/testcron.py >> /var/log/cron.log

I had copied most of the procedure from https://www.ekito.fr/people/run-a-cron-job-with-docker/ and assumed maybe there's a wire getting crossed since his tutorial is trying to output to console.

All credit to @brthornbury in comment to original question. Posting as answer for visibility for anyone else who stumbles across this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.