0

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.

X T
  • 445
  • 6
  • 22
  • How did you create the crontab if `vi` doesn't exist? – tink Jun 16 '22 at 17:21
  • See again the updated question. – X T Jun 16 '22 at 17:33
  • the permissions might be incorrect; try `-r-xr-xr-x` `RUN chmod +x /usr/local/src/script.go` (also note that golang is compiled, so it's really an executable, not a script as the name implies) – ti7 Jun 16 '22 at 17:50
  • @ti7 thanks for your answer. Should I change for same permissions of the json file that is used by the executable program as well? However, that permissions only add executable option to the owner but crontab, that will execute the program, already has executable permissions. Also, PATHs looks correct, isn't? – X T Jun 16 '22 at 18:01
  • it probably won't hurt (don't set execute, but read), though `root` should be able to read everything – ti7 Jun 16 '22 at 18:06
  • Have you done anything to start the `cron` daemon? – Hans Kilian Jun 16 '22 at 18:09
  • @HansKilian i just installed the packet and that’s it. That image is minimal so not sure how to enable the daemon through the same dockerfile on that base image… in case that this is the issue. – X T Jun 16 '22 at 18:11
  • Does this answer your question? [running a function periodically in go](https://stackoverflow.com/questions/40364270/running-a-function-periodically-in-go) – ti7 Jun 16 '22 at 18:49

2 Answers2

0

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

ti7
  • 16,375
  • 6
  • 40
  • 68
0

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.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • You are using a debian image but in the one i am working even running manually cron -f it does not recognizing the command. Also i cannot see anything inside /etc/init.d/crond no crond in that init.d – X T Jun 16 '22 at 18:31
  • If you update your post with a full Dockerfile based on `ubi8/ubi-minimal` I'll take a look at it. I don't know how to install packages on ubi-minimal, which is why I used debian. – Hans Kilian Jun 16 '22 at 18:40
  • @XT I figured it out and have updated my answer so it works on ubi-minimal. – Hans Kilian Jun 16 '22 at 18:51