Scheduling Cron Jobs on Business Days

Hello, is it possible to schedule cron jobs using business days instead of calendar days? I need to run several jobs on first and third business days of the month. I currently have this cron-tab entry which runs every week day at 5 AM. I need to schedule the same job on the 3rd Business day of the month. Any ideas to make this happen?

00 5 * * 1-5 pr_test.ksh >/dev/null 2>&1

Thank you.

Pramodini

I doubt cron will be versatile enough to schedule like this - even linux versions, but it could quite easily just call the script every day, and then you can provide the logic in the script to only run the required functionality if it meets the working day requirement.

Perhaps this would help..

If your system has cal command.

$ cal |awk 'NR>2 {print substr($0,4,14)}' |tr "\n" " " |awk '{printf "First business day is %s \nThird business day is %s \n", $1,$3}'

First business day is 1
Third business day is 3

Add above code in your script. compare with

date +%d

if they are same, run the script, otherwise, skip it.

@rdcwayx, I do not see �cal� command.
@citaylor, you are right, UNIX is not that versatile. I can handle the business days logic via K-Shell script, I was trying to avoid that.

Thank you both for the information.

Pramodini

check if this or this is useful

1 Like

As others have pointed out, I would run a script everyday. In that script check to see if it is the day you want the script to run, if not exit.

You're only talking about 24 days a year. Create a file with the days you want the script to run. Then grep the file using today's date. If today's date is in the file, continue the script. (If not exit). It could be as simple as:

DAY=`date +%d/%m/%Y`
RET_CODE=`grep -c $DAY /home/unxsa/holiday/holiday.date`
exit $RET_CODE

We use this not to run jobs on holidays.

cat /home/unxsa/holiday/holiday.date

01/01/2011
10/04/2011
13/04/2011
24/05/2010
01/07/2010
02/08/2010
06/09/2010
11/10/2010
11/11/2010
25/12/2010
26/12/2010

@purdym , I currently have this for not running the jobs on holidays and I plan to have a similar structure for business days as well.
Before the script below runs, I have a job that checks our Corporate Holidays calendar and creates a control file using holiday_ctrl.ctrl using SAS.

Thanks.

Pramodini

#!/usr/bin/ksh
#Pramodini Rode 12-22-2009 Initial Release
. ~/.profile

CTRL_FL_HOL=team/ctrl/holiday_ctrl.ctrl

if [[ -e $CTRL_FL_HOL ]]
then
print "Pramodini today is a holiday"
print "Exiting Program"
exit
else
print "Pramodini today is a working day"
print "Execute Program"
fi

Further to "purdym".
The whole year can be scheduled positively with 12 lines in crontab (each of which fires on two days-of-the-month within each the calendar month number). With this approach you must only specify day-of-month and not mention day-of-week or the cron will fire incorrectly.

Btw. The method using the unix "cal" command is awesome. Depending on the requirement it may go wrong if Bank Holidays are not deemed Business Days. Otherwise it seems perfect.

Thanks.

When I gave the solution, the poster didn't mention that we need exclude public holiday.

@ Pramodini Rode
Anyway if cal is not available, maybe there is similar command in your system, I give you the sample output of cal

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

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

So following my solution, we can do some adjusts to find out any business days.

cat /home/unxsa/holiday/holiday.date 

01/01/2011
10/04/2011
13/04/2011
24/05/2011
01/07/2011
02/08/2011
06/09/2011
11/10/2011
11/11/2011
25/12/2011
26/12/2011

you can run the script by: (only work in ksh, don't know why not work in bash)

for example, you need know the third business day in current month

./find_business_day 3

the 10th business day in August 2011

./find_business_day 10 8 2011

$ cat find_business_day

#! /usr/bin/ksh

Day=$(date +%d)
Month=$(date +%m)
Year=$( date +%Y)
HOLIDAY=/home/unxsa/holiday/holiday.date 
AWK=nawk

usage () {
  echo "Holiday date file at $HOLIDAY"
  echo "Usage: $0 Day [ Month Year ] "
  echo "example: "
  echo "get the 5th business day in current month"
  echo "$0 5 "
  echo "get the 10th bueinsee day on May 2010"
  echo "$0 10 5 2010"
}

if [[ $1 == "" ]]; then
  usage
  exit
else
  Bday=$1
fi

if [[ $3 == "" ]]; then
  CAL="/usr/bin/cal"
else
  Month=$2
  Year=$3
  CAL="/usr/bin/cal $Month $Year"
fi

Blist=$( $CAL |$AWK 'NR>2 {print substr($0,4,14)}' |tr "\n" " ")
$AWK -F \/ -v m=$Month -v y=$Year '$2==m&& $3==y {sub(/^0/,"",$1);print $1}' $HOLIDAY |while read line
do
  Blist=$(echo $Blist |$AWK -v d=$line '{for (i=1;i<=NF;i++) $i=($i==d)?"":$i}1' )
done

echo $Blist |$AWK -v d=$Bday '{print $d}'