Cron表达可以在不同的日子运行不同的月份 [英] Cron expression to run on different days for different months

查看:274
本文介绍了Cron表达可以在不同的日子运行不同的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下,我需要cron表达式:

I need a cron expression for the following situation:

我想在每个月的第一天的午夜制定时间表;但游行月份应该是例外.对于游行,应该在3月15日进行.

I want to run a schedule on midnight on every first day of every month; but march month should be an exception for that. For march, it should run on 15th of march.

推荐答案

我不认为您可以使用一个表达式来完成此操作,但是两个表达式都可以

I don't think you can do this with one expression, but two should be ok

0 0 1  1,2,4-12 * expr
0 0 15 3        * expr

即第一个表达式在三月份以外的所有月份运行.仅适用于3月.

i.e. the first expression runs for all months other than March. The second for March only.

或者,您可以指定脚本在的1号上运行,并且脚本可以检查给定月份的哪一天有效.那会给您一个表达,但是您将日期分析在cron和您的脚本之间进行了划分,因此,我认为这不是一个特别好的解决方案.对于更复杂的场景,值得牢记.

Alternatively, you could specify that your script runs on the 1st and 15th, and your script could check which day is valid given the month. That would give you one expression, but you're splitting the date analysis between cron and your script, and consequently I don't think that's a particularly good solution. It's worth bearing in mind for more complex scenarios.

这篇关于Cron表达可以在不同的日子运行不同的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆