0

I want to create a cronjob inside alpine image, which will run every 15 seconds. The command in dockerfile for this is

RUN echo "*/15 * * * * * /etc/init.d/aws-env-setup.sh" >> /var/spool/cron/crontabs/root

Below is the file after image start.

# do daily/weekly/monthly maintenance
# min   hour    day month   weekday command
*/15    *   *   *   *   run-parts /etc/periodic/15min
0   *   *   *   *   run-parts /etc/periodic/hourly
0   2   *   *   *   run-parts /etc/periodic/daily
0   3   *   *   6   run-parts /etc/periodic/weekly
0   5   1   *   *   run-parts /etc/periodic/monthly
*/15 * * * * * /etc/init.d/aws-env-setup.sh

The cronjob is not running.

How can I successfully create cronjob inside alpine containers. My aim is to run the cronjob only once, 15 seconds after container starts . Last line of my sh file, I want to run under cron is - crontab -r

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
Subit Das
  • 15
  • 4
  • Is cron running? Please extract a [mcve] first, including the full instructions you use to start it. Also, how do you determine that it "is not running"? – Ulrich Eckhardt Aug 11 '21 at 21:11
  • tl;dr: [cron does not provide sub-minute resolution](https://man7.org/linux/man-pages/man5/crontab.5.html). – Turing85 Aug 11 '21 at 21:25

1 Answers1

-1

I am annoyed by cron. It is so standard, so common, and should be so straight forward, but the reality is, that it to often just a mess. I formyself said screw cron, and make myself a python service based on this helper

def cron(delay, task):

    # delay in seconds
    next_time = time.time() + delay

    while True:
      try:
          task()
      except Exception:
          traceback.print_exc()
          print(str(traceback.print_exc()))

      next_time += (time.time() - next_time) // delay * delay + delay
      time.sleep(max(0, next_time - time.time()))

Set delay to 15, your task is

cmd = "/etc/init.d/aws-env-setup.sh"
task = subprocess.call(cmd, shell=True)

And make a service out of it. You can use your linux for it, but as you already work inside docker, you could set a docker restart policy and let docker take the work to handle it as daemon.

ScienceLover
  • 43
  • 1
  • 6
  • [cron does not provide sub-minute resolution](https://man7.org/linux/man-pages/man5/crontab.5.html). – Turing85 Aug 11 '21 at 21:25