Operating System - HP-UX
1832978 Members
2732 Online
110048 Solutions
New Discussion

Re: How to get first working day of the month

 
SOLVED
Go to solution
N Gopal
Frequent Advisor

How to get first working day of the month

Hi ,

I am executing one .scr script from 1-5(MON-FRIDAY)at 8:00 then 8:15 and so on. But it should execute one other fun on 1st working day of every month and if time is < 8:10.
I am having existing logic but it will work only 1st of the month it is working day.
From working day i mean (MON to FRI).

Existing logic in .scr file:

d means day, hr - hour, FIRSTRUNHOUR=8
*************************************
if [ $d -ne 1 -o $hr -ne $FIRSTRUNHOUR -o $min -ge 10 ]; then

break;

else

perform specific fun.
fi;

This 3rd of dec it did not execute.

Can some one suggest that how to do this?

Thanks
11 REPLIES 11
N Gopal
Frequent Advisor

Re: How to get first working day of the month

Hi,

More clarification on below query. I am running this cron everyday. But if this is first working day of the month it will do some extra function at first run of the cron means at 8:00. In second run 8:15 it should not perform the funtion, as mentioned in existing logic if it is > 8:10 the break.

Thanks
Yogeeraj_1
Honored Contributor

Re: How to get first working day of the month

hi,

Note that in the CRONTAB, you have the possibility to specify day of the month. Why dont you use this to schedule a script accordingly.

#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
00 08 1 * * /home/yogeeraj/myscript.sh 1>/home/yogeeraj/logfiles/output-myscript.crn 2>/home/yogeeraj/logfiles/error-myscript.crn
#*******************************************************************************


hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: How to get first working day of the month

hi,

Note that in the CRONTAB, you have the possibility to specify day of the month. Why dont you use this to schedule a script accordingly.

#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
00 08 1 * * /home/yogeeraj/myscript.sh 1>/home/yogeeraj/logfiles/output-myscript.crn 2>/home/yogeeraj/logfiles/error-myscript.crn
#*******************************************************************************


hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Srikanth Arunachalam
Trusted Contributor
Solution

Re: How to get first working day of the month

Hi,

A combination of cal and awk will return you the date of the first monday. If date on Monday falls on 3, that means to say saturday and sunday were 1st and 2nd. If 1st was as on Sunday, Monday would be 2nd. You can check the value of date of first monday using the below command.

first_monday=cal|awk '{print $2}'|head -4|tail -1

For all other days you can use the below logic.

day=$(date '+%d')
if [[ $day -eq 1 ]]
then
echo "execute for the 1st"
fi

Thanks,
Srikanth
Victor BERRIDGE
Honored Contributor

Re: How to get first working day of the month

Well what you were missing is adding again "almost" the same line in your cron but this time execution is base on week day (Mon) and you should test (in another script?)if day of month =2,3 then it should be executed ... (1 is treated in previous cron line...).

All the best
Victor
A. Clay Stephenson
Acclaimed Contributor

Re: How to get first working day of the month

There is another thing that could be a "gotcha". What should you do if the 1st Monday of a given month also happens to be a company holiday (e.g. Christmas, New Year's Day, ...).

Do a search for caljd.sh (or caljd.pl). This utility will easily handle your needs AND handle holidays.
If it ain't broke, I can fix that.
N Gopal
Frequent Advisor

Re: How to get first working day of the month

Hi All,

Very thanks for your valuable replies.
Actually thing is that i can not set cron for this otherwise it would be very tough to handle thing i have to write logic only.
I am quite convinced with Srinikant reply.
I am trying to implement it. I will come back in case of any issues if incounter.

Thanks
N Gopal
Frequent Advisor

Re: How to get first working day of the month

Hi Srikant,

Actually i want bit change in my query,

in everymonth i have to decide that among
1 or 2 or 3 which is first working day.
I have to put this logic in script that will be executed through cron everyday from 8 to 16.

Can you suggest anything?
Srikanth Arunachalam
Trusted Contributor

Re: How to get first working day of the month

Hi Gopal,

Put a simple if condition to check combination of day and date.
date +%a return the day of the week and
date +%d return the date as '01','02', '03' etc.

Hope this helps.

Regards,
Srikanth
Dennis Handly
Acclaimed Contributor

Re: How to get first working day of the month

>every month i have to decide that among 1 or 2 or 3 which is first working day.

We can only tell you the first weekday. You would have to translate "working" to your locale.

As Srikanth says, with a slight correction:
dow=$(date +%u) day of the week 1 M .. 7 S
dom=$(date +%d) day of month

So for first non-weekend day:
if [ $dom -eq 1 -a $dow -lt 6 -o
$dom -lt 4 -a $dow -eq 1 ]; then
echo "first workday in month"
else ...
N Gopal
Frequent Advisor

Re: How to get first working day of the month

Hi,

Thanks a lot it will work for me.

Many thanks