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

Re: cron: nth <weekday> of month?



on Fri, Apr 27, 2001 at 10:36:57AM -0500, Dave Sherohman (esper@sherohman.org) wrote:
> 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.)

Also nice.

I'm writing this up as a FAQ along the lines of "We should do this more
often -- running jobs on nth weekday of the mont and other cron
problems".

Interestingly, I'd just gone through a similar sort of problem trying to
identify last weekday of month using SAS, so the logic is similar.

Cheers.

-- 
Karsten M. Self <kmself@ix.netcom.com>    http://kmself.home.netcom.com/
 What part of "Gestalt" don't you understand?       There is no K5 cabal
  http://gestalt-system.sourceforge.net/         http://www.kuro5hin.org

Attachment: pgp95QiGXv9Nr.pgp
Description: PGP signature


Reply to: