'how to create a cron expression for every 2 weeks
Here is cron
expression I tried 0 0 0 */14 * ?
it is giving following schedules
Start Time:- Friday, September 8, 2017 1:25 AM
Next Scheduled :-
1. Friday, September 15, 2017 12:00 AM
2. Friday, September 29, 2017 12:00 AM
3. Sunday, October 1, 2017 12:00 AM
4. Sunday, October 15, 2017 12:00 AM
5. Sunday, October 29, 2017 12:00 AM
this expression is working for every 2 weeks in every month, but my requirement is it has to run for every 2 weeks, I mean after executing sept 29th
, nxt schedule should be October 13
but it is scheduling for October 1
Solution 1:[1]
There is no direct cron
expression for every 2 weeks but I kept following cron
expression , which is similar to 2 weeks but not exactly for 2 weeks
cron for every 2weeks(on 1st and 15th of every month at 1:30AM) - 30 1 1,15 * *
Solution 2:[2]
Friday every two weeks:
0 0 * * Fri [ $(expr $(date +%W) \% 2) -eq 1 ] && /path/to/command
Solution 3:[3]
You need to specify a start day. Otherwise it's will always reset with the 1st day of the month. So this expression "0 0 0 23/14 OCT ? 2017" is every 2 weeks starting on October 23rd 2017
Solution 4:[4]
30 7 1-7,14-21 * 1
“At 07:30 on every day-of-month from 1 through 7 and every day-of-month from 14 through 21 and on Monday.”
Solution 5:[5]
crontab manual on my Ubuntu 18 says:
Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).
and the mentioned example is:
# Run on every second Saturday of the month
0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sat |
Solution 2 | |
Solution 3 | Karl Fecteau |
Solution 4 | Cao Duy Võ Nguy?n |
Solution 5 | marcin007 |