我正在运行一个带有图像的码头集装箱:
ubi8/ubi-minimal
cronjob有正确的路径,go数据包已经安装:
crontab -l
*/2 * * * * go run /usr/local/src/script.go
该文件具有正确的权限:
-rw-r-xr-x 1 root root 6329 Jun 16 15:10 script.go
然而,crontab -e如下所示:
/bin/sh: /usr/bin/vi: No such file or directory
crontab: "/usr/bin/vi" exited with status 127
和
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
crontab被添加到dockerfile中,如下所示:
RUN crontab -l | { cat; echo "*/2 * * * * go run /usr/local/src/script.go"; } | crontab -
我认为这是正确的设置,不是吗?
crontab应该每2分钟执行一次脚本,但它不是。此外,图像是最小的,我不能编辑任何文件,我只是包含了一些来自dockerfile的文件的权限。
如果需要更改crontab中的任何路径,我必须通过dockerfile进行此操作。
发布于 2022-06-16 10:26:20
由于这听起来很麻烦,请考虑完全跳过cron守护进程,然后只在一个循环中睡觉。
#!/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
改编自
您可能会发现,通过使时间和目标可执行参数$1
$2
更好地扩展这一点。
发布于 2022-06-16 10:27:50
您需要启动cron守护进程。这是我制作的一个Dockerfile
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
注意,CMD运行crond
时,-n
选项将crond保持在前台。如果我们让它去线程化,那么docker将看到进程已经结束,并将终止容器。
我没有使用go,而是创建了一个类似于这样的小型shell脚本,名为script.sh
#/bin/sh
echo Hello from script >> ~/log.txt
它每2分钟写一次/root/log.txt。
https://stackoverflow.com/questions/72649498
复制相似问题