Saturday, October 29, 2022

[SOLVED] Verify crontab works on container using slim-buster docker image?

Issue

Problem Description

I am unable to see any output from the cron job when I run docker-compose logs -f cron after running docker-compose up.

When I attached to the container using VSCode, I navigated to var/logs/cron.log and ran the cat command and saw no output. Curiously, when I run crontab -l I see * * * * * /bin/sh get_date.sh as the output.

Description of Attempted Solution

Here is how I organized the project (it is over engineered at the moment for reasons of extensibility later)

├── config
│   └── crontab
├── docker-compose.yml
├── Dockerfile
├── README.md
└── scripts
    └── get_date.sh

Here is the details on the above, the contents are simple. Also, it is my attempt to use a lean python:3.8-slim-buster docker image so I can run bash or python scripts (not attempted):

crontab

* * * * * /bin/sh get_date.sh

get_date.sh

#!/bin/sh
echo "Current date and time is " "$(date +%D-%H:%M)"

docker-compose.yml

version: '3.8'
services:
  cron:
    build:
      context: .
      dockerfile: ./Dockerfile

Dockerfile

FROM python:3.8-slim-buster

#Install cron  
RUN apt-get update \
&& apt-get install -y cron 

# Copying script file into the container.
COPY scripts/get_date.sh .

# Giving executable permission to the script file.
RUN chmod +x get_date.sh

# Adding crontab to the appropriate location
ADD config/crontab /etc/cron.d/my-cron-file

# Giving executable permission to crontab file
RUN chmod 0644 /etc/cron.d/my-cron-file

# Running crontab
RUN crontab /etc/cron.d/my-cron-file

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Creating entry point for cron 
CMD ["cron", "tail", "-f", "/var/log/cron.log"]

Things Attempted

I am new in trying to get cron working in a container environment. I am not getting any error messages, so not sure how I can debug this issue except describe the behavior.

I have changed the content of crontab from * * * * * root bash get_date.sh to the above. I also checked out stackoverflow and found a similar issue here but no clear solution was proposed as far as I could tell.

Thanks kindly in advance.

References


Solution

You have several issues that are preventing this from working:

  1. Your attempt to run tail is a no-op: with your CMD as written you're simply running the command cron tail -f /var/log/cron.log. In other words, you're running cron and providing tail -f /var/log/cron.log as arguments. If you want to run cron followed by the tail command, you would need to write it like this:

    CMD ["sh", "-c", "cron && tail -f /var/log/cron.log"]
    
  2. While the above will both start cron and run the tail command, you still won't see any log output...because Debian cron doesn't log to a file; it logs to syslog. You won't see any output in /var/log/cron.log unless you have a syslog daemon installed, configured, and running.

I would suggest this as an alternative:

  1. Fix your syntax in config/crontab; for files installed in /etc/cron.d, you need to provide the username:

    * * * * * root /bin/sh /usr/local/bin/get_date.sh
    

    I'm also being explicit about the path here, rather than assuming our cron job and the COPY command have the same working directory.

    There's another problem here: this script outputs to stdout, but that won't go anywhere useful (cron generally takes output from your cron jobs and then emails it to root). We can explicitly send the output to syslog instead:

    * * * * * root /bin/sh /usr/local/bin/get_date.sh | logger
    
  2. We don't need to make get_date.sh executable, since we're explicitly running it with the sh command.

  3. We'll use busybox for a syslog daemon that logs to stdout.

That all gets us:

FROM python:3.8-slim-buster

# Install cron and busybox
RUN apt-get update \
  && apt-get install -y \
    cron \
    busybox

# Copying script file into the container.
COPY scripts/get_date.sh /usr/local/bin/get_date.sh

# Adding crontab to the appropriate location
COPY config/crontab /etc/cron.d/get_date

# Creating entry point for cron 
CMD sh -c 'cron && busybox syslogd -n -O-'

If we build an image from this, start a container, and leave it running for a while, we see as output:

Sep 22 00:17:52 701eb0bd249f syslog.info syslogd started: BusyBox v1.30.1
Sep 22 00:18:01 701eb0bd249f authpriv.err CRON[7]: pam_env(cron:session): Unable to open env file: /etc/default/locale: No such file or directory
Sep 22 00:18:01 701eb0bd249f authpriv.info CRON[7]: pam_unix(cron:session): session opened for user root by (uid=0)
Sep 22 00:18:01 701eb0bd249f cron.info CRON[8]: (root) CMD (/bin/sh /usr/local/bin/get_date.sh | logger)
Sep 22 00:18:01 701eb0bd249f user.notice root: Current date and time is  09/22/22-00:18
Sep 22 00:18:01 701eb0bd249f authpriv.info CRON[7]: pam_unix(cron:session): session closed for user root
Sep 22 00:19:01 701eb0bd249f authpriv.err CRON[12]: pam_env(cron:session): Unable to open env file: /etc/default/locale: No such file or directory
Sep 22 00:19:01 701eb0bd249f authpriv.info CRON[12]: pam_unix(cron:session): session opened for user root by (uid=0)
Sep 22 00:19:01 701eb0bd249f cron.info CRON[13]: (root) CMD (/bin/sh /usr/local/bin/get_date.sh | logger)
Sep 22 00:19:01 701eb0bd249f user.notice root: Current date and time is  09/22/22-00:19
Sep 22 00:19:01 701eb0bd249f authpriv.info CRON[12]: pam_unix(cron:session): session closed for user root


Answered By - larsks
Answer Checked By - Mary Flores (WPSolving Volunteer)