Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS cron expression to run every other Monday

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.

like image 743
thehme Avatar asked Jul 23 '20 16:07

thehme


2 Answers

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.

like image 151
Chris Williams Avatar answered Sep 28 '22 11:09

Chris Williams


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.

like image 34
Dennis Traub Avatar answered Sep 28 '22 11:09

Dennis Traub