How to run a cron job inside a docker container

两盒软妹~` 提交于 2019-12-17 21:51:34

问题


I tried to run a cron job inside a docker container

but nothing works for me

my container have only cron.daily and cron.weekly file

crontab,cron.d,cron.hourly ... are absent in my container

crontab -e also not working

my container runs with /bin/bash


回答1:


Here is how I run one of my cron containers.

Dockerfile:

FROM alpine:3.3

ADD crontab.txt /crontab.txt
ADD script.sh /script.sh
COPY entry.sh /entry.sh
RUN chmod 755 /script.sh /entry.sh
RUN /usr/bin/crontab /crontab.txt

CMD ["/entry.sh"]

crontab.txt

*/30 * * * * /script.sh >> /var/log/script.log

entry.sh

#!/bin/sh

# start cron
/usr/sbin/crond -f -l 8

script.sh

#!/bin/sh

# code goes here.
echo "This is a script, run by cron!"

Build like so

docker build -t mycron .

Run like so

docker run -d mycron

Add your own scripts and edit the crontab.txt and just build the image and run. Since it is based on alpine, the image is super small.




回答2:


thanks for this template.

I only wonder about one line in entry.sh

/usr/sbin/crond -f -L 8

crond -help yields:

Usage: crond -fbS -l N -d N -L LOGFILE -c DIR

        -f      Foreground
        -b      Background (default)
        -S      Log to syslog (default)
        -l N    Set log level. Most verbose:0, default:8
        -d N    Set log level, log to stderr
        -L FILE Log to FILE
        -c DIR  Cron dir. Default:/var/spool/cron/crontabs

so maybe you wanted to rather put the small l

/usr/sbin/crond -f -l 8

istead of the big 'L'

/usr/sbin/crond -f -L 8

to set the log level to default, because specifying an logfile called 8 does not seem to be intended.




回答3:


crond works well with tiny on Alpine

RUN apk add --no-cache tini

ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/usr/sbin/crond", "-f"]

but should not be run as container main process (PID 1) because of zombie reaping problem and issues with signal handling. See this Docker PR and this blog post for details.



来源:https://stackoverflow.com/questions/37015624/how-to-run-a-cron-job-inside-a-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!