2
votes

I want to build a cron expression for following - which should run every two weeks and start from specific day of month.

I tried to build expression like this -

5 4 2/14 * * * (Here i want to run cron at 04:05 on every 14th day-of-month from 2 through 31)

Can somebody help?

2
Your question is tagged with ruby-on-rails and ruby – do you want to build the crontab line(s) programmatically? - Stefan
You can try crontab.guru for creating CRON expression. - Gokul
@Stefan I am using cron2english gem to cron cron expression to human readable format. So that's why tagged it with ruby and ruby-on-rails. And while parsing the cron expression it is giving me the invalid error. - palash-kulkarni

2 Answers

2
votes

So, you want it to run on the 2nd, then 14 days later, and again 14 days later? As this is not an enormous list, just give that list:

5 4 2,16,30 * *
0
votes
5 4 * * 1 test $(($(date +%W)%2)) -eq 1 && your_command

Ok, the tricky part to make it run every two weeks is to know what day of the week is the first 2nd. Let's imagine it is a Monday, this monday, and this week (number 13). We we'll focus on make it run every second monday.

5 4 * * 1 Every monday script or command works at 04:05

$(($(date +%W)%2)) Evaluates the numeric week expresion and gets his module (1 odd or 0 par)

test $(($(date +%W)%2)) -eq 1 True if the week is odd, we avoid par weeks. We can change it to -eq 0 in case of need it to work on par weeks

&& your_command If the previous condition works, your script or command runs