Reputation: 407
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?
Upvotes: 2
Views: 729
Reputation: 704
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 * *
Upvotes: 2
Reputation: 430
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
Upvotes: 0