0

Can I get help composing a cron schedule? The schedule is supposed to run a command at the following times:

  • last 2 days of 1st week of every month
  • last 2 days of 3rd week of every month
3
  • 2
    Neither of your criteria are definitive. Are you using ISO 8601 terminology? This states specifically "Weeks start with Monday and end on Sunday", so the last two days are Saturday and Sunday. Also "The ISO standard does not define any association of weeks to months. A date is either expressed with a month and day-of-the-month, or with a week and day-of-the-week, never a mix." The first week of a year contains the first Thursday of a year, so you might (informally) use the same criteria for the first week of the month. Dec 21, 2022 at 0:15
  • What if a month starts on 7th day of the week? Will you ignore such week or have just one run instead of two?
    – White Owl
    Dec 21, 2022 at 0:19
  • .... so your days would be the 3rd, 4th, 17th, 18th of December 2022, and the 7th, 8th, 21st, 22nd of January 2023. The first and last weeks of the year are especially strange, as ISO 8601 uses leap-weeks, not leap-days. Dec 21, 2022 at 0:23

1 Answer 1

1

ISO 8601 defines the first week of the year as the week which includes January 4th or the week which has >= four days of the new year in it (it also defines that weeks start on Monday so both definitions are the same). It doesn't have a specific definition of "1st week of a month", put proposes to apply the same "has >= four days of the month" rule.

This doesn't directly help you because Cron doesn't know about this and has no concept of "1st week of a month" or "3rd week of a month". What you can do instead is having the script run on the last two days of each week with

0 1 * * 6,7 /path/to/my/script

and then do some date-based calculations (depending on your definition of "first week of a month") to determine whether the script should terminate immediately or actually run through.

Using the ">= four days of the month" rule from above, you could use something like the following in /bin/sh

d=$(date +%d)
if [ "$d" -ge 3 ] && [ "$d" -le 10 ]; then
    # 1st week
elif [ "$d" -ge 17 ] && [ "$d" -le 24 ]; then
    # 3rd week
else
    exit
fi
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .