6

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.

Chris Williams
  • 32,215
  • 4
  • 30
  • 68
thehme
  • 2,698
  • 4
  • 33
  • 39
  • 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. – stdunbar Jul 23 '20 at 16:47

3 Answers3

6

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.

Chris Williams
  • 32,215
  • 4
  • 30
  • 68
  • 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. – thehme Jul 23 '20 at 17:18
  • Great, yeah cron expressions are limited unfortunately :( – Chris Williams Jul 23 '20 at 17:19
  • Why not something like `0 14 * * 2#1,2#3 *` or `0 14 * * 2#1#3 *`? – Ganesh Satpute Jul 24 '20 at 09:46
  • This might work, it would need to be tested in CloudWatch Events @GaneshSatpute. At least it would reduce the 2 rules down to 1 :) – Chris Williams Jul 24 '20 at 10:05
  • I tried creating this but it didn't work. I think it would have been very easy to support this. – Ganesh Satpute Jul 25 '20 at 13:41
  • Hmm, perhaps something to raise as an improvement :) – Chris Williams Jul 25 '20 at 13:46
  • @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 ` – Ganesh Satpute Jul 27 '20 at 13:31
  • Oops, updated now @GaneshSatpute. It seems it needs the `?` for the date field. – Chris Williams Jul 27 '20 at 13:40
  • 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. – Ganesh Satpute Jul 27 '20 at 16:16
  • 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: https://aws.amazon.com/premiumsupport/knowledge-center/send-feedback-aws/ – Chris Williams Jul 27 '20 at 16:26
6

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.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
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.
Yee Lee
  • 1
  • 2
  • Hi Yee, you could improve your answer by explaining each bit of the cron entry for people that are newbies. – Rohit Gupta Jul 09 '23 at 13:38
  • 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. https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html#cron-based – Yee Lee Jul 10 '23 at 15:59
  • 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. – Rohit Gupta Jul 11 '23 at 11:14