使用 Kubernetes 将 CronJob 安排在每月的最后一天
我们需要使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
date
命令来控制是否执行另一个脚本或命令:您可以使用此逻辑和您的
CronJob
在每月的28-31日运行,但仅在明天是当月 1 日时才执行脚本:此外,如果基础映像中的
date
版本存在差异并且不支持-d +1day ,只需将日期检查卸载到您的实际容器作业中,并让您的作业检查“明天”是否是第一天。
You can use the
date
command to control whether or not to execute another script or command: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: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.您可以设置表达式 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 示例:
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: