From: underh20 on
On May 19, 2:47 pm, Bit Twister <BitTwis...(a)mouse-potato.com> wrote:
> On Wed, 19 May 2010 14:20:30 -0700 (PDT), underh20 wrote:
> > Hello,
>
> > Our server is running Solaris 10. I need  to run a cron job at 6am on
> > last Sunday of every month starting from the last Sunday this month in
> > May.
>
> Subtract last day of month from current day of month.
> if result greater than 6 exit, else must be last Sunday in month.
>
> man date   to fetch current day of month.
>
> For last day of month us cal and shift. Starter snippet to play with.
>
> set -- $(cal)
> let x="$# - 1"
> shift $x
> echo

Hi Bit Twister,

Thanks for your suggestion. I actually need to know how to set it up
in cron to run this script
at 8am on last Sunday of each month, i.e., what to put in the
"minute", "hour", "day of the month"
, "month of the year" and "day of the week" fields in cron. Any
idea ?

* * * * * /usr/bin/script

Thanks,

Bill
From: Andrew L. on
I don't believe you can get that specific with cron saying "run on the
last Sunday of the month". You could say "run on the 28th of every
month at 8 am":

0 8 28 * * /script

(may or may not be a Sunday)

You could say "run on Sunday of every month but only where Sunday is the
20-25th day of the month"

0 8 20-25 * 0 /path/to/script.sh

I think the best method would be to say "run on every Sunday of the month:"

0 8 * * 0 /path/to/script.sh

Then within your script, before it executes, have verbage there to check
if it in fact is the last Sunday of the month, it not, exit script.

- Andrew L.

underh20 wrote:
> On May 19, 2:47 pm, Bit Twister <BitTwis...(a)mouse-potato.com> wrote:
>> On Wed, 19 May 2010 14:20:30 -0700 (PDT), underh20 wrote:
>>> Hello,
>>> Our server is running Solaris 10. I need to run a cron job at 6am on
>>> last Sunday of every month starting from the last Sunday this month in
>>> May.
>> Subtract last day of month from current day of month.
>> if result greater than 6 exit, else must be last Sunday in month.
>>
>> man date to fetch current day of month.
>>
>> For last day of month us cal and shift. Starter snippet to play with.
>>
>> set -- $(cal)
>> let x="$# - 1"
>> shift $x
>> echo
>
> Hi Bit Twister,
>
> Thanks for your suggestion. I actually need to know how to set it up
> in cron to run this script
> at 8am on last Sunday of each month, i.e., what to put in the
> "minute", "hour", "day of the month"
> , "month of the year" and "day of the week" fields in cron. Any
> idea ?
>
> * * * * * /usr/bin/script
>
> Thanks,
>
> Bill
From: underh20 on
On May 20, 7:27 am, Guy <Use-Reply-To-Address-Header@[127.1]> wrote:
> underh20 wrote:
> > Our server is running Solaris 10.
> > I need to run a cron job at 6am on last Sunday of
> > every month starting from the last Sunday this
> > month in May.
>
> > The current cron job below runs on every Sunday.
> > How could we modify it to run on last Sunday only
> > in each month for the next 3 years ?
>
> > 0 6 * * 0   /usr/bin/script
>
> The folowing will fail in the year 2032
> due to Leap Day falling upon Sunday.
>
> 0 6 22-28 2 0 /usr/bin/script
> 0 6 24-30 4,6,9,11 0 /usr/bin/script
> 0 6 25-31 1,3,5,7,8,10,12 0 /usr/bin/script
>
> (So you're good for the next 3 years)


****************************************************
Another possible option ??
****************************************************

Thanks Guy's for his excellent suggestion. I've been experimenting
with the "cal" command
where I just grab the date for last Sunday of the month. I would like
to keep the job in cron as is
"0 6 * * 0   /usr/bin/script" which runs on every Sunday and the
script will have logic to only run
itself if today's date is the last Sunday of each month.

Please critique my program below. I'd like to get feedback from you
experts out there :-)
Thanks, Bill


< body of /usr/bin/script>

#!/bin/sh

LAST_SUNDAY=`cal | cut -c1-2|grep -v "^$"|tail -1`
DAY=`date +%d`
if [$LAST_SUNDAY = $DAY]
then
#
# <body of codes to be executed>
#
exit 0
else
echo "EXIT!! Today is not last Sunday of month"
fi
From: webjuan on
On May 19, 5:20 pm, underh20 <underh20.scubadiv...(a)gmail.com> wrote:
> Hello,
>
> Our server is running Solaris 10. I need  to run a cron job at 6am on
> last Sunday of every month starting from the last Sunday this month in
> May.
>
> The current cron job below runs on every Sunday.  How could we modify
> it to run on last Sunday only in each month for the next 3 years ?
> Thanks, Bill
>
> 0 6 * * 0   /usr/bin/script

This came from the UGU mailing list. Hope it helps.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

UNIX GURU UNIVERSE
UNIX HOT TIP

Unix Tip 2935 - February 18, 2010

http://www.ugu.com/sui/ugu/show?tip.today

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


RUN ON LAST SUNDAY

If you want a job to run on the last
sunday of every month, you can use
the following syntax from within cron:

18 * * * 0 [`date "+%d"` -gt 24] && /path/to/script

i.e. on sundays at 18:00 check if
the day of the month is greater than
24 - if so run the job (if 23 is
specified the job will run on the last
2 sundays of the month)

NOTE: There back-ticks around the date
command, not single quotes.

This tip generously supported by: duncan.ferguson(a)egg.com

juan martinez
From: Andrew L. on
underh20 wrote:
> On May 20, 7:27 am, Guy <Use-Reply-To-Address-Header@[127.1]> wrote:
>> underh20 wrote:
>>> Our server is running Solaris 10.
>>> I need to run a cron job at 6am on last Sunday of
>>> every month starting from the last Sunday this
>>> month in May.
>>> The current cron job below runs on every Sunday.
>>> How could we modify it to run on last Sunday only
>>> in each month for the next 3 years ?
>>> 0 6 * * 0 /usr/bin/script
>> The folowing will fail in the year 2032
>> due to Leap Day falling upon Sunday.
>>
>> 0 6 22-28 2 0 /usr/bin/script
>> 0 6 24-30 4,6,9,11 0 /usr/bin/script
>> 0 6 25-31 1,3,5,7,8,10,12 0 /usr/bin/script
>>
>> (So you're good for the next 3 years)
>
>
> ****************************************************
> Another possible option ??
> ****************************************************
>
> Thanks Guy's for his excellent suggestion. I've been experimenting
> with the "cal" command
> where I just grab the date for last Sunday of the month. I would like
> to keep the job in cron as is
> "0 6 * * 0 /usr/bin/script" which runs on every Sunday and the
> script will have logic to only run
> itself if today's date is the last Sunday of each month.
>
> Please critique my program below. I'd like to get feedback from you
> experts out there :-)
> Thanks, Bill
>
>
> < body of /usr/bin/script>
>
> #!/bin/sh
>
> LAST_SUNDAY=`cal | cut -c1-2|grep -v "^$"|tail -1`
> DAY=`date +%d`
> if [$LAST_SUNDAY = $DAY]
> then
> #
> # <body of codes to be executed>
> #
> exit 0
> else
> echo "EXIT!! Today is not last Sunday of month"
> fi

I don't see any reason why that wouldn't work. For those non-*nix-saavy
that want perhaps an easier to read/understand method at the sacrifice
of lines of code:

#!/bin/bash
currentday=`date +%d`
month=`date +%b`
dayofweek=`date +%w`
case "$month" in
Jan)
daysinmonth=31;
;;
Feb)
daysinmonth=28;
;;
Mar)
daysinmonth=31;
;;
Apr)
daysinmonth=30;
;;
May)
daysinmonth=31;
;;
Jun)
daysinmonth=30;
;;
Jul)
daysinmonth=31;
;;
Aug)
daysinmonth=31;
;;
Sep)
daysinmonth=30;
;;
Oct)
daysinmonth=31;
;;
Nov)
daysinmonth=30;
;;
Dec)
daysinmonth=31;
;;
*)
echo "Month format not recognized."
exit
;;
esac
flag=$(($daysinmonth - $currentday))
if [ "$flag" -gt "6" ]; then
echo "It's nowhere close to the last Sunday of the month"
exit
else
if [ $dayofweek = 0 ]; then
echo "It's the last Sunday of the month! Run code!"
else
echo "It's close to the last Sunday of the month, but
not quite there!"
exit
fi
exit
fi