[PLUG] cron on first tuesday or last friday of the month?

Paul Heinlein heinlein at attbi.com
Sat Aug 17 17:47:54 UTC 2002


On Sat, 17 Aug 2002, Richard Seymour wrote:

> Can cron jobs be configured to run on the Nth specific weekday of
> each month?

No, not without help.

We've got a regular mailing at work that goes out every other week; 
the only way to accomplish that trick is to have the script 
use 'at' to schedule itself to run again in 2 weeks.

In your case, I'd probably have a cron job that runs every Thursday,
but have the script (or whatever) check to see if the current date is 
within the acceptable range, e.g.,

  today=$(date +%d)

  if [ $today -lt 15 -o $today -gt 21 ]; then
    # don't need to run today
    exit 0
  fi

  # rest of script  

Alternatively, you can use a more complex check with something like
the Perl Date::Manip module.

> Besides the Nth specific weekday problem, I also need to be able to
> run jobs on the last specific weekday, which will be either the
> fourth or the fifth, depending on the calendar.

This one is also fairly easy to solve using GNU date. Just do 
something like this every Friday (or whatever):

  monththisweek=$(date +%m)
  monthnextweek=$(date --date'+ 1 week' +%m)

  if [ $monththisweek -eq $monthnextweek ]; then
    # there are more of this day in the month; don't need to run
    exit 0
  fi

  # rest of script

--Paul Heinlein <heinlein at attbi.com>






More information about the PLUG mailing list