[linux-elitists] cron (and other tricks): nth day, last day scripts

Karsten M. Self kmself@ix.netcom.com
Mon Mar 21 17:18:49 PST 2005


There's a recurring question of how to run cron jobs at specific
intervals -- the nth weekday of the month, or the last given weekday of
the month.

Playing with the 'date' command gives several useful tests which can be
run.  In a bash 'if/then' clause, actions can be taken.  Returning a
true/false value allows the result itself to be tested directly and
concisely on a crontab entry.

Both scripts make use of a bashism, the $() notation, keep in mind if
porting to other shells.


------------------------------------------------------------------------
nthday
------------------------------------------------------------------------
#!/bin/bash
# Karsten M. Self Mon Mar 21 16:57:30 PST 2005
# Free use, distribution, and modification with no restrictions, granted.
# NO WARRANTY
# ------------------------------------------------------------------------
# Test if today is the nth weekday of the month.
# ------------------------------------------------------------------------

export PATH=/usr/bin:/bin

NTHDAY=$1

if [ $(( ( ( $( date +%d) + 1 ) /7 ) )) -eq $NTHDAY ]
    then true
    else false
fi
------------------------------------------------------------------------

Usage:

    # Run if today is the last given weekday of month
    $ lastday && command

    # Crontab: Run on last Thursday of month.
    1 0 * * 4   lastday && command      


------------------------------------------------------------------------
lastday
------------------------------------------------------------------------
#!/bin/bash
# Karsten M. Self Mon Mar 21 16:57:30 PST 2005
# Free use, distribution, and modification with no restrictions, granted.
# NO WARRANTY
# ------------------------------------------------------------------------
# Test if today is the last given weekday of month
# ------------------------------------------------------------------------

export PATH=/usr/bin:/bin

if [ $( date -d '+ 7 days' +%d) -lt $( date +%d ) ]
    then true
    else false
fi
------------------------------------------------------------------------

Usage:

   # Run if today is the 3rd given weekday of month
   $ nthday 3 && command

   # Crontab:  Run on 3rd Monday of the month:
    1 0 * * 1   nthday 3  && command      


Both scripts are available at:

    http://linuxmafia.com/~karsten/Download/lastday
    http://linuxmafia.com/~karsten/Download/nthday


Copy 'em to /usr/local/bin/


Peace.

-- 
Karsten M. Self <kmself@ix.netcom.com>        http://kmself.home.netcom.com/
 What Part of "Gestalt" don't you understand?
    gconf-editor:  reimplementation of the MS Windows Registry for
    GNU/Linux, with the concommitant problems of undocumented settings,
    cryptic keys, inability to comment settings, and use of a single,
    specialized application to access the configuration settings.
    - Karsten M. Self
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://allium.zgp.org/pipermail/linux-elitists/attachments/20050321/e1a4612e/attachment.pgp 


More information about the linux-elitists mailing list