# How to execute a script on 2. last Workday of month only?

How to execute a script on 2. last Workday of month only?

With workdays, regular business days was meant.

So, i will explain the way to solve this requirement.
For some statistic jobs it may neccessary to start a script a business day eayrlier than last day of a month.
The first answer is how to get out if today is the last day of month.

• We want to keep it simple
• Find a general rule

Without writing code for the calendar rules (keep it simple) like 30 or 31 days a month or leap-year and February, we can use the intelligence / knowledge of command date.

• The last day of a month will be the day before first day of month.

## How will it help us in the requirement to execute a script only on 2nd business day of week?

Well a first we need a uniqe reference value.

• echo \$(date +%u) # returns day of week. Monday = 1 Sunday = 7
• echo \$(date +%d) # returns day of month.

This two values will help us to calculate backwards to get out if today is the last day of month. The rule is simple if tomorrow is the first day of a month, today is the last day of a month.

• if [ \$(date +%d -d „1 day“) -eq 1 ] ;then  echo „today is the last day of month“; fi

## How will it help us now with 2. last businessday?

It’s important to have a reference date at first. But, lets specify business day now:
Monday – Friday, Weekday 1 – 5. But this solution doesn’t handle public holidays. Be aware of that and it may differ to some cultures and business.

So, to be 2nd last business day in month, there must follow a business day within this month. From Monday to Thursday (Weekday 1-4) there will be always a following business day on next day.  Only on Friday there are 2 more days. So we have a simple rule now!

## Summary:

1. 2nd last working day is from weekday 1-4 (lower 5), if tomorrow =  day of month 1.
2. on friday (weekday 5) the 1st of month must be in 4 days.

This bash example will solve it now:

# get out the weekday of today:

WEEKDAY=\$(date +%e)

# AND Weekday is 5 (Friday).

if [ \$(date +%d -d „2 day“) -eq 1 ] && [ \${WEEKDAY} -lt 5 ] || [ \$(date +%d -d „5 day“) -eq 1 ] && [ \${WEEKDAY} -eq 5 ] ; then
echo „This is the 2. last working day of the month“
fi