If adding a command that repeats every 10 minutes

2019-02-19 22:07发布

As part of the setup of a docker container, the following gets injected into crontab:

*/10 * * * * /opt/run.sh >> /opt/run_log.log

According to the behavior of crontab, when should the first run kick off? Should the 10 minute cycle begin instantly, or 10 minutes after this is put into crontab. Neither behavior is happening so I am trying to debug this in more depth by trying to understand the intended behavior.

1条回答
放我归山
2楼-- · 2019-02-19 22:46

This cron sandbox simulator gives you an idea:

Mins    Hrs Day Mth DoW
*/10    *   *   *   *

This run time (UTC)     Sat 2016-Jan-23 0653
Forward Schedule    Sat 2016-Jan-23 0700
    Sat 2016-Jan-23 0710
    Sat 2016-Jan-23 0720

It uses the syntax:

Every nth '0-23/n', '*/2' would be every other.
'*/1' is generally acceptable elsewhere, but is flagged here as possibly an unintended entry.


See for example "Run a cron job with Docker" (by Julien Boulay)

Let’s create a new file called “crontab” to describe our job.

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.

The following DockerFile describes all the steps to build your image

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

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

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

Then you can build the image with

sudo docker build --rm -t ekito/cron-example .

And run it:

sudo docker run -t -i ekito/cron-example

Be patient, wait for 2 minutes and your commandline should display:

Hello world
Hello world

If you replaced the first '' by '/10', you would have to wait to the next 0 or 10 or 20 or... of the hour.

查看更多
登录 后发表回答