使用 Kubernetes 将 CronJob 安排在每月的最后一天

发布于 2025-01-17 18:19:50 字数 101 浏览 4 评论 0原文

我们需要使用Kubernetes的Cron调度程序在本月的最后一天自动运行工作。例如,在三月,它应在3月31日下午6点进行。在4月,应在4月30日下午6点举行。任何帮助都将受到赞赏。谢谢。

We have a requirement where we have to run our job automatically on the last day of the month using the cron scheduler in Kubernetes. For example, for March, it should run on March 31st at 6pm. For April, it should run on April 30th at 6pm. Any help is appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2025-01-24 18:19:50

您可以使用date命令来控制是否执行另一个脚本或命令:

# Tested using GNU coreutils date 8.30
/bin/sh -c "[ $(date -d +1day +%d) -eq 1 ] && echo 'It is the last day of the month!'"

# It is the last day of the month!

您可以使用此逻辑和您的CronJob在每月的28-31日运行,但仅在明天是当月 1 日时才执行脚本:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  # Run at 6pm on the 28th-31st of each month
  schedule: "0 18 28-31 * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cronjob
            image: busybox:1.35.0
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - "[ $(date -d +1day +%d) -eq 1 ] && job.sh"
          restartPolicy: OnFailure

此外,如果基础映像中的 date 版本存在差异并且不支持 -d +1day ,只需将日期检查卸载到您的实际容器作业中,并让您的作业检查“明天”是否是第一天。

You can use the date command to control whether or not to execute another script or command:

# Tested using GNU coreutils date 8.30
/bin/sh -c "[ $(date -d +1day +%d) -eq 1 ] && echo 'It is the last day of the month!'"

# It is the last day of the month!

You can use this logic and your CronJob to run on the 28th-31st of each month, but only execute your script if tomorrow is the 1st of the month:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  # Run at 6pm on the 28th-31st of each month
  schedule: "0 18 28-31 * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cronjob
            image: busybox:1.35.0
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - "[ $(date -d +1day +%d) -eq 1 ] && job.sh"
          restartPolicy: OnFailure

Additionally, if there's a discrepancy on your version of date inside your base image and it doesn't support -d +1day, just offload the date check into your actual container job and have your job check to see if "tomorrow" is the 1st.

冬天旳寂寞 2025-01-24 18:19:50

您可以设置表达式 cron job“0 18 28-31 * *”
它将在“28 日至 31 日每天 18:00”运行。在源代码中,让我们检查 time.Now + 1。如果 day of time.Now + 1 == 1,则调用函数来处理逻辑。

使用 github.com/robfig/cron/v3 的 golang 示例:

_, _ = c.AddFunc("0 18 28-31 * *", func() {
    tomorrow := time.Now().AddDate(0, 0, 1)
    if tomorrow.Day() == 1 {
        fmt.Println("This is the last day of the month")
    }
})

You can set the expression cron job "0 18 28-31 * *"
It will run "At 18:00 on every day of the month from 28 through 31." In your source code, let's check time.Now + 1. If day of time.Now + 1 == 1, you call your function to process logic.

Example golang using github.com/robfig/cron/v3:

_, _ = c.AddFunc("0 18 28-31 * *", func() {
    tomorrow := time.Now().AddDate(0, 0, 1)
    if tomorrow.Day() == 1 {
        fmt.Println("This is the last day of the month")
    }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文