Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute Script on First working day 1

Status
Not open for further replies.

dbadmin

Programmer
Jan 3, 2003
147
US
Hi,

I need to execute a script on the first working day (M-F) of every month. I tried to configure cron and it doesn't allow me to do this. Is there any way I could do it?

Thanks,
dbadmin.
 
You want to execute the job if it's the 1st of the month and a working day, or if it's the 2nd or 3rd and a Monday, right?

You could write a little script (call it 'wd1') like:
[tt]
#!/bin/bash

DOM=`date +%e`
DOW=`date +%a`
exit $( \
[ $DOM -eq 1 -a $DOW != 'Sat' -a $DOW != 'Sun' ] \
|| [ $DOM -le 3 -a $DOW == 'Mon' ]
[/tt]
Then you could set the following command to run every day (or just the 1st 3 days of the month):
[tt]
wd1 && monthlyjobscript
[/tt]
 
Sorry, I forgot a closing bracket at the end of that exit statement:
[tt]
#!/bin/bash

DOM=`date +%e`
DOW=`date +%a`
exit $( \
[ $DOM -eq 1 -a $DOW != 'Sat' -a $DOW != 'Sun' ] \
|| [ $DOM -le 3 -a $DOW == 'Mon' ] )
[/tt]
 
cron should work

1. crontab -e
* * * * 1-5 <absolute path for script>
2. wq!

The fifth file indicates the days of the week 0 to 6 for Sunday to Saturday
 
GrahamBright, that won't do it. That will cause it to run every weekday in the month, not just the first working day in the month.
 
Hi all,

I got the answer from another forum and it goes like this. Schedule the job in cron like this

00 08 1-7 * * x=`date +"%A"` ; [[ $x == "Monday" ]] && Script.sh 2>&1

which will do the trick.

Thanks any way for your valuable time and post. You all are awesome.

Thanks again
dbadmin
 
dbadmin,

this solution will run your script on the first Monday of every month.
This is not what you asked for in your first post.
Did you change your mind in the meantime?

regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top