Operating System - HP-UX
1748098 Members
5703 Online
108758 Solutions
New Discussion

Re: cron job to run on the last business day of the month

 
Nick24
Visitor

cron job to run on the last business day of the month

Hi Experts,

I need to schedule a cron job to run on the last "working day" (last business day excluding weekend ) of the month. Please can someone advise me on how to achieve that?

Thanks all

8 REPLIES 8
Nick24
Visitor

Re: cron job to run on the last business day of the month

Re: cron job to run on the last business day of the month

I've created a below script and i dont know if this is the right approach. Please can you experts help me to get this to work if this is thre only best way to achieve what im after?

Here I am defining the las working day for all months in the script .

thanks

#!/usr/bin/bash
typeset -i x1_day_of_month=`date +"%d"`
typeset -i x1_month_of_year=`date +"%b"`
typeset -i x1_year=`date +"%Y"`

x1_datestring=`$x1_day_of_month $x1_month_of_year $x1_year`
x1_listofdates="
30Sep2019


if [ "$x1_D" = "$x1_$listofdates"] ; then
echo "Run job"

elseif [ $x1_FLAGRUN != 1 ] ; then
echo "do not run fancyjob today"
exit

Steven Schweda
Honored Contributor

Re: cron job to run on the last business day of the month

> [...] i dont know if this is the right approach. [...]

   I would say that running the script every day, and letting it decide
if Today is the right day, is a good approach.

> [...] Here I am defining the las working day for all months in the
> script .

   Manually creating a list of dates seems to me to be asking for
trouble.  A modern "date" command (with a "v" option) offers many
helpful features.

   I don't have an HP-UX system running at the moment, but the following
method seems to work on a Mac:

#!/bin/sh

# Start of test loop.
ofs=1                                   # Offset (months), for testing.
while test ${ofs} -lt 24 ; do


# Actual last-workday-of-month calculation.

dow=8                                   # "%u": 1=Mon - 7=Sun.
back=0                                  # Days back from end-of-month.

while test ${dow} -gt 5 ; do            # Want day-of-week Mon-Fri (1-5).
    back=` expr ${back} + 1 `           # Increment back step.

    date -v1d -v+${ofs}m -v-${back}d    # End of this month less "back" days.
                                        # (If "ofs" == 1.)
    dow=` date -v1d -v+${ofs}m -v-${back}d +%u `        # Day of week.

echo "dow = ${dow}"

done

# Date strings for comparison with Today.
   lwd_dat=` date -v1d -v+${ofs}m -v-${back}d +%G-%m-%d `
   tod_dat=` date +%G-%m-%d `

echo ${lwd_dat}
### echo ${tod_dat}


# End of test loop.

echo ''

ofs=` expr $ofs + 1 `                   # Increment offset months.

done
Bill Hassell
Honored Contributor

Re: cron job to run on the last business day of the month

Here's a simpler way:

[[ $(date +%d) != $(cal | awk '{ if(NF>1) a=$NF ; if (NF==7) a=$6}END{print a}') ]] &&
exit

Schedule your script to run every day.
Put this test at the start of your script.
It will always exit if today's date is not the last business day of the month.

Here's a snippet to show the calendar for each month and the last business day of the month:

 

cal $(date +%Y)
for MON in 1 2 3 4 5 6 7 8 9 10 11 12
do
   LASTBUSDAY=$(cal $MON $(date +%Y) | awk '{ if(NF>1) a=$NF ; if (NF==7) a=$6}END{print a}')
   echo "$MON/$LASTBUSDAY \c"
done
echo

 

Looks like this for 2019:

 

                                2019

         Jan                    Feb                    Mar
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
       1  2  3  4  5                   1  2                   1  2
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    3  4  5  6  7  8  9
13 14 15 16 17 18 19   10 11 12 13 14 15 16   10 11 12 13 14 15 16
20 21 22 23 24 25 26   17 18 19 20 21 22 23   17 18 19 20 21 22 23
27 28 29 30 31         24 25 26 27 28         24 25 26 27 28 29 30
                                              31
         Apr                    May                    Jun
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
    1  2  3  4  5  6             1  2  3  4                      1
 7  8  9 10 11 12 13    5  6  7  8  9 10 11    2  3  4  5  6  7  8
14 15 16 17 18 19 20   12 13 14 15 16 17 18    9 10 11 12 13 14 15
21 22 23 24 25 26 27   19 20 21 22 23 24 25   16 17 18 19 20 21 22
28 29 30               26 27 28 29 30 31      23 24 25 26 27 28 29
                                              30
         Jul                    Aug                    Sep
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
    1  2  3  4  5  6                1  2  3    1  2  3  4  5  6  7
 7  8  9 10 11 12 13    4  5  6  7  8  9 10    8  9 10 11 12 13 14
14 15 16 17 18 19 20   11 12 13 14 15 16 17   15 16 17 18 19 20 21
21 22 23 24 25 26 27   18 19 20 21 22 23 24   22 23 24 25 26 27 28
28 29 30 31            25 26 27 28 29 30 31   29 30

         Oct                    Nov                    Dec
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
       1  2  3  4  5                   1  2    1  2  3  4  5  6  7
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    8  9 10 11 12 13 14
13 14 15 16 17 18 19   10 11 12 13 14 15 16   15 16 17 18 19 20 21
20 21 22 23 24 25 26   17 18 19 20 21 22 23   22 23 24 25 26 27 28
27 28 29 30 31         24 25 26 27 28 29 30   29 30 31




1/31 2/28 3/29 4/30 5/31 6/28 7/31 8/30 9/30 10/31 11/29 12/31

 



Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: cron job to run on the last business day of the month

> Here's a simpler way:

   And that's why "if this is thre only best way" is seldom a good
question.

Nick24
Visitor

Re: cron job to run on the last business day of the month

Hi Bill,

thanks for your response. The snippet output is as below bu 3/30 is a sat as well as 9/28 \c?

 

1/31 \c
2/28 \c
3/30 \c
4/30 \c
5/31 \c
6/29 \c
7/31 \c
8/31 \c
9/28 \c
10/31 \c
11/30 \c
12/31 \c

Nick24
Visitor

Re: cron job to run on the last business day of the month

Hi Steven, I am getting the below error. Any advice?

thanks

date: invalid option -- 'v'
Try 'date --help' for more information.
date: invalid option -- 'v'
Try 'date --help' for more information.
dow =
./lastwday_test.sh: line 13: test: -gt: unary operator expected
date: invalid option -- 'v'
Try 'date --help' for more information.

Steven Schweda
Honored Contributor

Re: cron job to run on the last business day of the month

> date: invalid option -- 'v'

   Hmmm.  I did say "A modern "date" command (with a "v" option) [...]".
I assumed that it might be a GNU "date" feature, but I don't see it
there (GNU coreutils), either.  GNU "date" does have a "--date" option
which might be able to do similar things.  I'd need to investigate
further.

      https://www.gnu.org/software/coreutils/manual/coreutils.html#Options-for-date

   Aside from building it from source, GNU coreutils for HP-UX should be
available at/near:

      http://hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-8.31/

Bill Hassell
Honored Contributor

Re: cron job to run on the last business day of the month

>> 3/30 and 9/28

Not sure how you got these values.
I show the vaues in my post: 3/29 and 9/30.
Are you using the standard HP-UX shell /usr/bin/sh? 
What version of HP-UX are you running?

I've tested this awk procedure on HP-UX 10.20 through 11.31.



Bill Hassell, sysadmin