AWS cron expression to run every other Monday
Asked Answered
W

4

8

I want to schedule a CloudWatch event to run every other Monday and have started with this command:

0 14 ? * 2 *

Currently with the above command, I get a weekly schedule of Monday executions:

Mon, 27 Jul 2020 14:00:00 GMT
Mon, 03 Aug 2020 14:00:00 GMT
Mon, 10 Aug 2020 14:00:00 GMT
Mon, 17 Aug 2020 14:00:00 GMT
Mon, 24 Aug 2020 14:00:00 GMT
Mon, 31 Aug 2020 14:00:00 GMT
Mon, 07 Sep 2020 14:00:00 GMT
Mon, 14 Sep 2020 14:00:00 GMT
Mon, 21 Sep 2020 14:00:00 GMT
Mon, 28 Sep 2020 14:00:00 GMT

However, I would like the schedule to be set to every other Monday, e.g.

Mon, 27 Jul 2020 14:00:00 GMT
Mon, 10 Aug 2020 14:00:00 GMT
Mon, 24 Aug 2020 14:00:00 GMT
Mon, 07 Sep 2020 14:00:00 GMT
Mon, 21 Sep 2020 14:00:00 GMT

I have seen examples with exp and # being used, but I don't think AWS CloudWatch events accept these sort of parameters.

Writhe answered 23/7, 2020 at 16:34 Comment(1)
It might be "cheating" but you'd be better off creating a rate expression of 14 days on a Monday. Cron expressions would be more difficult. – Unlookedfor
A
8

You won't be able to do any of the fancier commands (especially those using variables from the command line).

You could do this very basically but would require 2 separate events in order to carry it out:

  • 0 14 ? * 2#1 * - Run on the first Monday of the month.
  • 0 14 ? * 2#3 * - Run on the third Monday of the month.

Unfortunately there is no compatible syntax for scheduled expressions that would allow the concept of every other week, so the above commands occasionally could lead to a 3 week gap.

If you don't care about the Monday you could of course use 0 14 1,15 * * to run on the 1st and 15th of each month (roughly every 2 weeks).

The final option would be to run every Monday, but have the script exit if it is not the every other week, the expression would then just be 0 14 ? * 2 *.

More information about the syntax is available on the Cron Expressions section of the Scheduled Events page.

Algolagnia answered 23/7, 2020 at 16:53 Comment(10)
I wonder how long before CloudWatch events support more complex syntax 😞 but thanks for confirming this. I think I will have to settle for not caring about Monday executions 😞 until I can figure out if @dennis-traub suggestion works. Just got to get this out asap. – Writhe
Great, yeah cron expressions are limited unfortunately :( – Algolagnia
Why not something like 0 14 * * 2#1,2#3 * or 0 14 * * 2#1#3 *? – Guacin
This might work, it would need to be tested in CloudWatch Events @GaneshSatpute. At least it would reduce the 2 rules down to 1 :) – Algolagnia
I tried creating this but it didn't work. I think it would have been very easy to support this. – Guacin
Hmm, perhaps something to raise as an improvement :) – Algolagnia
@ChrisWilliams I checked whether the expresssion from your answer works or not, but 0 14 * * 2#1 * also gives me an error Details: Parameter ScheduleExpression is not valid... I'm creating expression by Going to CloudWatch > Events > Rules > Choosing source as Schedule, entering corn expression as 0 14 * * 2#1 * then click on Configure Details > Create Rule – Guacin
Oops, updated now @GaneshSatpute. It seems it needs the ? for the date field. – Algolagnia
Yes. Now it worked with basic case. It's quite strange though. There seems to be no consistency. This works for 0 14 ? * 1-3#1 *, giving schedule of Mon-Tue. But 0 14 ? * 1,3#1 * doesn't. – Guacin
This is likely the parser used for the cron expressions. If you send feedback it should back to the teams :). This can be done here: aws.amazon.com/premiumsupport/knowledge-center/… – Algolagnia
A
8

Chris' answer is correct. Currently, there is no way that I could think of to express this as part of CloudWatch Scheduled Events.

However, a workaround could be to set it to every Monday (0 14 ? * 2 *) and trigger a Lambda function that checks whether it's in the on-week or the off-week before triggering the actual target.

Even though this adds some complexity, it would be a viable solution.

Ayo answered 23/7, 2020 at 17:2 Comment(0)
A
1

You can do this with a rate scheduler and setting the start date.

In AWS SAM it would look like this for example:

  Events:
    CWSchedule:
      Type: ScheduleV2
      Properties:
        ScheduleExpression: "rate(14 days)"
        FlexibleTimeWindow:
          Mode: FLEXIBLE
          MaximumWindowInMinutes: 5
        StartDate: "2024-04-01T12:00:00.000Z"
        ScheduleExpressionTimezone: UTC

See here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-schedulev2.html

And here: https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateSchedule.html#scheduler-CreateSchedule-request-StartDate

Ahead answered 4/4 at 12:22 Comment(0)
B
0

Adding on to @Chris answers in the bullet points,

one can add in additional rule to accommodate for months that have 5 Mondays

  • 0 14 ? * 2#5 * - Run on the FIFTH Monday of the month.
Broody answered 5/7, 2023 at 19:18 Comment(3)
Hi Yee, you could improve your answer by explaining each bit of the cron entry for people that are newbies. – Valdivia
Hi @Rohit Gupta, the first bit refers to the Minutes (0-59), second bit is Hours (0-23), third bit is Day-of-Month (1-31) with a (?) meaning any day of the month, forth bit is Month (1-12 or JAN-DEC) , with an asterisk () meaning all values, fifth bit mean Day-of-week (1-7 or SUN-SAT), the pattern 2#5 with hatch(#) wildcard means you are selecting Monday (2) and it has to be the fifth(5) Monday of the month. Last bit refers to the Year and with () meaning the rule is effective for all years. See aws ref. docs.aws.amazon.com/scheduler/latest/UserGuide/… – Broody
Hi Yee, thats not for me, I have been using cron for 30 years. You should consider enhancing your answer by explaining it to others that follow. Then I might consider a vote. – Valdivia

© 2022 - 2024 β€” McMap. All rights reserved.