skip to Main Content

I’m running a docker container with an image:

ubi8/ubi-minimal

The cronjob has correct path and go packet is already installed:

crontab -l
*/2 * * * * go run /usr/local/src/script.go

The file has correct permissions:

-rw-r-xr-x 1 root root 6329 Jun 16 15:10 script.go

However the crontab -e is like this:

/bin/sh: /usr/bin/vi: No such file or directory
crontab: "/usr/bin/vi" exited with status 127

and

cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

The crontab was added in the dockerfile like this:

RUN crontab -l | { cat; echo "*/2 * * * * go run /usr/local/src/script.go"; } | crontab -

I think is correctly setup isn’t it?

the crontab should execute the script every 2 minuts but it’s not. Also the image is minimal and I cannot edit any file I just included some permissions to the files from the dockerfile.

If needed to change any Path from crontab I have to do this trough the dockerfile.

2

Answers


  1. As it sounds like a lot of trouble, consider skipping the cron daemon entirely and just sleep in a loop

    #!/bin/sh
    while true; do
      TIME_LOOP_START=$(date +%s)  # integer time in seconds
      script.go
      # calculate offset for 2 minutes in seconds
      sleep $(($TIME_LOOP_START + 120 - $(date +%s)))
    done
    

    adapted from

    You may find this is even better extended by making the time and target executable arguments $1 $2

    Login or Signup to reply.
  2. You need to start the cron daemon. Here’s a Dockerfile I made to illustrate

    FROM registry.access.redhat.com/ubi8/ubi-minimal
    RUN microdnf update && microdnf install cronie
    RUN crontab -l | { cat; echo "*/2 * * * * /usr/local/src/script.sh"; } | crontab -
    COPY script.sh /usr/local/src/
    CMD crond -n
    

    Note that the CMD runs crond with the -n option which keeps crond in the foreground. If we let it daemonize, docker would see that the process had ended and would terminate the container.

    Instead of using go, I made a small shell script like this, called script.sh

    #/bin/sh
    echo Hello from script >> ~/log.txt
    

    It writes to /root/log.txt every 2 minutes.

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