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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cron: run first Thursday of every month 2

Status
Not open for further replies.

vuakhobo

Technical User
Apr 22, 2004
41
0
0
US
Is there a way to set job to run first Thursday of every month?

I found this on other thread and wonder if it is correct?
0 2 * * 4 [ `date '+%e'` -le 7 ] && run_script.sh
 
execute a script the first Thursday of the month: (This is untested):

Code:
  1   0  1,2,3,4,5,6,7  *   4   Your script

  |   |     |           |   |
  M   H   Day of        M   Day of Week
  I   O    Month        O
  N   U                 N
      R                 T
                        H

                        Of
                        Y
                        E
                        A
                        R
 
Looks fine to me olded, although 1-7 should do instead of 1,2,3....


Annihilannic.
 
Olded, that doesn't work. It's been discussed here before. We summarized it then that cron can't handle this.

Your crontab entry would execute at 00:01 on the 1st to 7th of every month AND on every thursday of every month. Cron doesn't check ALL the date fields... If ANY of the DayOfMonth OR DayOfWeek field checks out, then it's OK for cron.

In order to limit to 1st thursday, you need something like vuakhobo found while searching (in this forum.)

==> run a script on the 1st to 7th of every month. Either in that script or in the crontab entry commandline, check if *today* is a thursday, continue, if *today* is any other day, exit.


HTH,

p5wizard
 
Yep, even says so on the man page (from HP-UX):

separated by a hyphen (meaning an inclusive range). Note that the
specification of days can be made in two fields: monthday and weekday.
If both are specified in an entry, they are cumulative. For example,

0 0 1,15 * 1 command

runs command at midnight on the first and fifteenth of each month, as
well as every Monday. To specify days in only one field, set the
other field to asterisk (*). For example,

0 0 * * 1 command

runs command only on Mondays.

Annihilannic.
 
I stand corrected, then this probably works:

Code:
     0 2 1-7 * *   [ "$(date +%a)" == "Thu" ] && myscript

as it is a variation of the original post. I learned something.
 
Olded, I still have some remarks

1. [tt]test[/tt] or [tt][ ... ][/tt] string equality is with a single =, not C-like ==

2. [tt]date +'%a'[/tt] gives abbreviated weekday names, but those names/abbreviations are dependent on what [tt]LANG[/tt] is in effect for [tt]cron[/tt]. You're better of using [tt]date +'%u'[/tt] and test for equal to 4


Code:
0 2 1-7 * *   [ $(date +%u) -eq 4 ] && myscript

See man pages for [tt]test[/tt] and [tt]date[/tt]


HTH,

p5wizard
 
Just to add, if using dates in cron, remember to escape all of the % signs - \%

I want to be good, is that not enough?
 
Except that it isn't correct...

AIX cron is no different from any other UNIX cron.

from man crontab on AIX 5.3:

[tt] ... The specification
of days may be made by two fields (day of the month and day of the
week). If you specify both as a list of elements, both are adhered
to. For example, the following entry:

0 0 1,15 * 1 command

would run command [red]on the first and fifteenth days[/red] of each month, [red]as
well as
every Monday.[/red] To specify days by only one field, the other
field should contain an * .[/tt]



HTH,

p5wizard
 
I'm with p5, the URL provided by Mike points to erroneous info.
 
Ah well, it just goes to show one can't believe everything that's posted on the internet!

I want to be good, is that not enough?
 
And I didn't plainly believe the man page either, I went ahead and tried out what was suggested on that page.

[tt]15 2 1-7 * 4 /path/to/my/testscript[/tt]

My testscript ran today at 2:15 (every/any thursday, not just the first thursday of this month). It would also run on 1st to 7th of next month... Except that I already removed it from the crontab ;-)



HTH,

p5wizard
 
If you read the page I posted, You'll see

"The following description applies only to the Korn Shell Script Implementation of crond named "ecrond_k93"."





Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Yes Mike, you are right, I should have spotted that...

...but I have never before heard or seen, let alone used ecrond_k93.


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top