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

Re: [OT] Cron and day of the week



On Wednesday, 25.03.2009 at 16:25 +0000, T o n g wrote:

> I know we can put day of the week info as cron schedules, but how can I 
> define:

None of these are simple cron entries.  I work with similar rules as
follows:

> - first Monday of the month

Run the cron job on the 1st to 7th of the month, where cron calls a
wrapper shell script with something like this:

    #!/bin/bash
    #
    # From 'man date'
    #
    #  %w     day of week (0..6);  0 represents Sunday

    REQUIRED_DAY=1

    TODAY=`date +%w`

    if [ $TODAY -eq $REQUIRED_DAY ]; then
        echo The actual thing you want to happen is here
    fi

> - second Monday of the month

As above, but cron job runs on 8th to 14th of each month.

> - last Monday of the month

That's harder - the *last* Monday is less clear, because of varying
numbers of days in the month. e.g. Monday 23 February 2009 was the last
Monday in February 2009, but Monday 30 March 2009 will be the last
Monday of this month (and the *fifth* Monday in March).

(Of course, if by 'last' you really mean 'fourth', then you can run your
wrapper script on 21st to 28th of each month.)

Hope that helps,

(You can also reverse the logic of the cron calls if you like, by running
the job on every Monday and then using the wrapper script to determine
if its between the 1st and 7th of the month etc.)

Dave.

-- 
Dave Ewart
davee@ceu.ox.ac.uk
Computing Manager, Cancer Epidemiology Unit
University of Oxford / Cancer Research UK
PGP: CC70 1883 BD92 E665 B840 118B 6E94 2CFD 694D E370
Get key from http://www.ceu.ox.ac.uk/~davee/davee-ceu-ox-ac-uk.asc
N 51.7516, W 1.2152

Attachment: signature.asc
Description: Digital signature


Reply to: