0

Warning: k8s greenhorn on this side.

I need to run a task that will be set up in a k8s cronjob. I need it to run every 45 minutes. Having this in the schedule does not work:

0/45 * * * *

Because it would run at X:00, then X:45 then X+1:00 instead of X+1:30. So I might need to set up multiple schedule rules instead:

0,45 0/3 * * *
30   1/3 * * *
15   2/3 * * *

I am wondering if it's possible to set up multiple schedules in a single CronJob definition or if I will have to setup multiple CronJobs so that each CronJob takes care of each line.

https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/cron-job-v1/

Update: I just read that it's possible to have more than a single manifest written in a single yaml file so it might work with 3 manifests.... but knowing if it's possible with a single manifest would be awesome.

2 Answers 2

0

Try */45, because according to manual 0/45 means every other 45 of 0...

7
  • I thought of that but it would mean that it would run right away (or almost) and then every 45 minutes, right?
    – eftshift0
    Oct 28, 2021 at 16:02
  • It is hard to say. Documentation is not clear about it. Some realization of cron ported to windows says that */x means to calculate from crontab service start and 0/x to calculate from the beginning of hour. Unfortunately I just can suggest to test this...
    – Saboteur
    Oct 28, 2021 at 16:16
  • 1
    The minutes expansion is relative to a single hour. */28 runs at 0, 28, 56, 0, 28, 56 ... Easy to test with a command like date >> myCron.log. It never uses service start time. I also believe your hour repeats have to be a range, like 2-23/3, because step operates on a list of times. Oct 28, 2021 at 22:13
  • I don't think hours should be range, but I believe when you are using */x, x should be less then maximum allowed (60 mins, 24 hours) and it will not overcalculate the maximum
    – Saboteur
    Oct 29, 2021 at 20:51
  • @Saboteur man -s 5 crontab on Linux. "Step values can be used in conjunction with ranges." My crontab will not even accept minutes as 21/10 -- it will take 21-21/10. Oct 29, 2021 at 22:18
0

If the crontab specification appears to be incomplete or ambiguous, finding out what it really does can clarify the documentation. All tests made on Linux Mint 19.3 (in which crontab is POSIX-compliant).

0/40 * * * * date >> Cron.log

crontab -e will not even permit the above one-line crontab. The 0 is a specific minute. It makes no sense to put a skip on that. It throws:

"/tmp/crontab.4BQ7AN/crontab":0: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? (y/n) 

Contrast with

*/40 * * * * date >> Cron.log

It accepts this version, because the * represents a list of 0,1,2,...,59. The skip reduces this to 0 and 40.

I put together a crontab intended to demonstrate the effects of range and step constructs.

$ crontab -l
#.. Crontab to illustrate ranges and steps.

#.. Every 20 mins from 0: 0, 20, 40.
*/20       13,14 * * * ~/Stamp 'Line  4:: */20'

#.. Every 20 minutes from 5: 5, 25, 45.
5-59/20    13,14 * * * ~/Stamp 'Line  7:: 5-59/20'

#.. Every 7 minutes from 9 to 35: 9, 16, 23, 30.
9-35/7     13,14 * * * ~/Stamp 'Line 10:: 9-35/7'

#.. Every 13 minutes from 33 to 59: 33, 46, 59.
33-59/13   13,14 * * * ~/Stamp 'Line 13:: 33-59/13'

#.. Once only.
14-14/2    13,14 * * * ~/Stamp 'Line 16:: 14-14/2'

#.. Once only.
11-59/999  13,14 * * * ~/Stamp 'Line 19:: 11-59/999'

~/Stamp is a shell script that logs the time it ran, and the minutes value from the crontab file.

$ cat ~/Stamp
#! /bin/bash
#: Stamp: demonstrate crontab processing.

Log="./133000.cronLog"
printf >>"${Log}" '%(%T)T cron time spec %s\n' -1 "${1}"

I installed the crontab at 13:35, and it logged these tasks. It stopped logging at 14:59 because of the hours 13,14 values, thereby running over an hour boundary. I believe the hours, dayofmonth, month and dayofweek columns work identically, but a 90-minute test is enough for me.

paul@paul-RV415-RV515 ~ $ tail -F 133000.cronLog
tail: cannot open '133000.cronLog' for reading: No such file or directory
tail: '133000.cronLog' has appeared;  following new file
13:40:01 cron time spec Line  4:: */20
13:45:01 cron time spec Line  7:: 5-59/20
13:46:01 cron time spec Line 13:: 33-59/13
13:59:01 cron time spec Line 13:: 33-59/13
14:00:01 cron time spec Line  4:: */20
14:05:01 cron time spec Line  7:: 5-59/20
14:09:01 cron time spec Line 10:: 9-35/7
14:11:01 cron time spec Line 19:: 11-59/999
14:14:01 cron time spec Line 16:: 14-14/2
14:16:01 cron time spec Line 10:: 9-35/7
14:20:01 cron time spec Line  4:: */20
14:23:01 cron time spec Line 10:: 9-35/7
14:25:01 cron time spec Line  7:: 5-59/20
14:30:01 cron time spec Line 10:: 9-35/7
14:33:01 cron time spec Line 13:: 33-59/13
14:40:01 cron time spec Line  4:: */20
14:45:01 cron time spec Line  7:: 5-59/20
14:46:01 cron time spec Line 13:: 33-59/13
14:59:01 cron time spec Line 13:: 33-59/13
^C
paul@paul-RV415-RV515 ~ $ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.