[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: cron: nth <weekday> of month?



On Thu, Apr 26, 2001 at 12:32:46PM -0700, Karsten M. Self wrote:
> Another missing specification:  does anyone have a good "last weekday of
> month" recipie?

'Last weekday of the month' is equivalent to 'Monday - Thursday where
tomorrow is next month or Friday where 3 days from now is next month'.

So...  Set up two cron entries, one for mon-thu, one for fri.
(To additionally optimize, run the mon-thu job only on the 28th-31st of
the month and the fri job on the 26th-31st.)  Based off of Rob's solution
to the original question, use test on mon-thu whether `date +%d -d +1day`
== 1 and on fri whether `date +%d -d +3day` < 4.

That gives us:

1 0 28-31 * 1-4 [ "$(date +%d -d +1day)" -eq "1" ] && command -args
1 0 26-31 * 5   [ "$(date +%d -d +3day)" -lt "4" ] && command -args

(-lt was used instead of < because it < does an ascii comparison -
"29" < "4".  -eq was used instead of == to be consistent; either should
work in that case.)

-- 
That's not gibberish...  It's Linux. - Byers, The Lone Gunmen
Geek Code 3.1:  GCS d? s+: a- C++ UL++$ P++>+++ L+++>++++ E- W--(++) N+ o+
!K w---$ O M- V? PS+ PE Y+ PGP t 5++ X+ R++ tv b+ DI++++ D G e* h+ r y+



Reply to: