k8s cronjob 中单个任务的多个计划

k8s cronjob 中单个任务的多个计划

警告: 这边是k8s新手。

我需要运行一个在 k8s cronjob 中设置的任务。我需要它每 45 分钟运行一次。将此放在其中schedule不起作用:

0/45 * * * *

因为它会运行于X:00, thenX:45X+1:00不是X+1:30。所以我可能需要设置多个计划规则:

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

我想知道是否可以在一个计划中设置多个计划单身的CronJob 定义,或者我是否必须设置多个 CronJob,以便每个 CronJob 处理每一行。

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

更新:我刚刚读到,可以在单个 yaml 文件中写入多个清单,因此它可以与 3 个清单一起使用......但是知道是否可以使用单个清单会很棒。

答案1

尝试 */45,因为根据手册,0/45 意味着每隔 45 个 0...

答案2

如果 crontab 规范看起来不完整或含糊不清,找出它的真正用途可以澄清文档。所有测试均在 Linux Mint 19.3 上进行(其中 crontab 符合 POSIX 标准)。

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

crontab -e 甚至不允许上面的一行 crontab。 0 是特定的分钟。跳过它是没有意义的。它抛出:

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

对比

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

它接受这个版本,因为 * 代表 0,1,2,...,59 的列表。跳跃将其减少到 0 和 40。

我编写了一个 crontab 来演示范围和步骤构造的效果。

$ 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 是一个 shell 脚本,用于记录其运行时间以及 crontab 文件中的分钟值。

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

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

我在 13:35 安装了 crontab,它记录了这些任务。由于 13,14 小时的值,它在 14:59 停止记录,因此运行时间超过了一个小时边界。我相信小时、月份、月份和星期几列的工作方式相同,但 90 分钟的测试对我来说就足够了。

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 ~ $ 

相关内容