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!

crontab entry 3

Status
Not open for further replies.

Mag0007

MIS
Feb 15, 2005
829
US
Is there an algorithm to schedule something as first business day of the month?

 
man cron
man date

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Phv:

Thanks for the quick response :)

I know how to use crontab and date. Its just getting it so I can use it on a offical first business day of the month.

I was thinking of something like this

0 6 1 * 1 foo > /dev/null

Run foo at 6:00AM everymonth, on the first day?

 
from man 5 crontab
[tt]
field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)
[/tt]

So 1st of every month should be:

0 6 1 * * foo > log.log 2>1&

Cheers.
 
Right, thanks.

But if the first day is Sunday, thats no good for me. I need this to run on the first buisness day (Monday). Is that possible?
 
And foo may test with the date command if it has to sleep until a business day ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV:

Now I think I know what you are saying. Thats a very good idea!

 
to get the 1st monday of every month in the US locale:
Code:
cal 8 2004| nawk 'FNR > 2 && NF >= 6 {print (NF==6) ? $1 : $2; exit}'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Here's a simpler way to run something the 1st Monday of every month...
Code:
0 6 1-7 * 1 foo > foo.log 2>1&
The "[tt]1-7[/tt]" limits it to the first 7 days of the month. The "[tt]1[/tt]" limits it to a Monday. This only works if a Monday occurs within the first 7 days of the month. [bigsmile]

Now the first business day is just the first Monday through Friday that occurs in a month. You can do this with the following crontab (it takes three entries to do it though)...
Code:
0 6 1 * 1-5 foo > foo.log 2>1&
0 6 2 * 1   foo > foo.log 2>1&
0 6 3 * 1   foo > foo.log 2>1&
The first line means if the 1st falls on a Monday through Friday, run it. The second line means if it's the 2nd and it's a Monday, run it. This means the 1st was on a Sunday. The third line means if it's the 3rd and it's a Monday, run it. This is when the 1st is a Saturday.

So, these three lines will run "[tt]foo[/tt]" on the first bsuiness day of the month (assuming a business day is Monday through Friday).

Hope this helps.
 
SamBones, I'm not sure ...
manpage said:
Note that the specification of days may be made by two fields
(day of the month and day of the week). If both are specified as
a list of elements, both are adhered to. For example, 0 0 1,15 *
1 would run a command on the first and fifteenth of each month,
as well as on every Monday. To specify days by only one field,
the other field should be set to `` * '' (for example, 0 0 * * 1
would run a command only on Mondays).

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hmmm, you're right! Well that s*cks! How do I return the star I got? [smile]

Then it needs to be done in the script. Something like this as the first few lines in the script...
Code:
#!/bin/ksh

export DOM=$(date '+%d')
export DOW=$(date '+%a')

(( ${DOM} > 3 )) && exit
[[ "${DOW}" = Sat || "${DOW}" = Sun ]] && exit
[[ "TueWedThuFri" = +(*${DOW}*) && ${DOM} != 01 ]] && exit

# If it reaches here, it's the first business day of the month.
# Your first business day code starts here...
Then, you'd just need to run the script with the following [tt]crontab[/tt] entry...
Code:
0 6 1-3 * * first_bus_day.sh > fbd.log 2>&1
That will run it on the 1st, 2nd, and 3rd, but it will only get past those lines if it's the first business day of the month.

Hope this helps.
 
Heh, what if the first monday is a holiday? :)
Why not each monday of this month, find out what the first monday is of the next, and [green]at[/green] the job instead?

You could then use this to figgure out the next one:
Code:
cal [red]$(($(date +%m)+1))[/red] [red]$(date +%Y)[/red]| nawk 'FNR > 2 && NF >= 6 {print (NF==6) ? $1 : $2; exit}'

And after getting that into $x (not that hard, I'll leave it to you):
Code:
at $(($(date +%m)+1))/$x/$(date +%Y) -f nameOfThisScript

[plug=shameless]
[/plug]
 
Sorry, everyone works on holidays due to scripting issues! [bigsmile]

jstreich, your examples won't work in December. You'll be trying to get a calendar for the 13th month. [smile] Try this...
Code:
cal $(( ($(date +%m)+1) % 12 )) $(( $(date +%Y) + ($(date +%m)/12) ))
...to get the next month with a correct rollover to the next year.

If Monday is a holiday, the first business day for the month would be the Tuesday immediately following it, not the first Monday of the next month.

I *HATE* problems that seem simple at first, but get messier and messier as you dig into them.

I worked at a place once where we had this issue and the cleanest solution (for us at the time) ended up being a calendar table in an Oracle database that had all the days in the year with holidays and other special processing days flagged.

Hope this helps.
 
Sambones:

"I *HATE* problems that seem simple at first, but get messier and messier as you dig into them."

BINGO!!!

I like how PHV was like, "man cron, man date" :)

 
I like how PHV was like, "man cron, man date"
I was lazzy, the true answer (for me) was:
man cron
man date
man sleep
 
@samBones:

Keep the star from me.

I first asked myself 'hey, don't they work ORed (day of month, day of week) but your explanation was so well done, that I forgot my doubt. :)

But we get so seldom stars, when they would have been appropriate, so we can keep the few, that were spent by accident. ;)

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top