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

Crontab entry for every Tuesday on an even week - evaluate format %W 6

Status
Not open for further replies.

bazil2

Technical User
Feb 15, 2010
148
DE
(Elementary user)

Can anyone please help me with the following; I would like to execute a script on Tuesday's but only in even weeks.

In my effort below I am using trying to evaluate the %W format hence my '?'. Is there a way of evaluating as to whether the week is odd or even?

00 06 * * 2 ([ "`date +\%W`" -eq ? ] && ( /path/to/script ))

Best regards
 
Hi

In Bash and (M)Ksh I would try it like this :
Code:
00 06 * * 2 (( $( date +'%w' )%2 )) && /path/to/script
( By the way, first check if [tt]date[/tt] is found or need to write it as [tt]/usr/bin/date[/tt] . )


Feherke.
 
What I think I'd consider is to have a 'marker' file containing just the week number, to be incremented each week (on Sunday/Monday perhaps) via cron. Then have your script check whether the content of the marker file is divisible by 2 to an integer and if it is run your script, aborting if it isn't. Hope this makes sense - post back if more info. is needed.

The internet - allowing those who don't know what they're talking about to have their say.
 
Good morning,

Many thanks for your help.

As a layman could I ask you please if my interpretation of the crontab entry is correct:

00 06 * * 2 (( $( date +'%w' )%2 )) && /path/to/script

Basically the crontab will try and execute at 6am every Tuesday, however it first evaluates the date. The format %w means 'check that it's a Tuesday' but how does it evaluate whether the week number is even?

Best regards
 
Hi

bazil2 said:
how does it evaluate whether the week number is even?
man bash | SHELL GRAMMAR | Compound Commands said:
[tt]((expression))
The expression is evaluated according to the rules described
below under ARITHMETIC EVALUATION. If the value of the
expression is non-zero, the return status is 0; otherwise the
return status is 1. This is exactly equivalent to let
"expression".[/tt]
Let us see it in examples, step by step :
Code:
[blue]master #[/blue] date +'%W'
Wed May 11 11:14:13 EEST 2011

[blue]master #[/blue] date +'%W'
19

[gray]# so our arithmetical evaluation is this :[/gray]
[blue]master #[/blue] echo $(( 19%2 ))
1

[gray]# next week will change to this :[/gray]
[blue]master #[/blue] echo $(( 20%2 ))
0

[gray]# next weeks, just to emphasize the modulo operation : [/gray]
[blue]master #[/blue] echo $(( 21%2 ))
1

[blue]master #[/blue] echo $(( 22%2 ))
0

[gray]# the same expression without the result returned :[/gray]
[blue]master #[/blue] (( 19%2 ))

[gray]# no output, but the exit code was set :[/gray]
[blue]master #[/blue] echo $?
0

[gray]# next week will be evaluated like this :[/gray]
[blue]master #[/blue] (( 20%2 ))

[blue]master #[/blue] echo $?
1

[gray]# we can use that exit code to condition the execution of the second command in the list :[/gray]
[blue]master #[/blue] (( 19%2 )) || echo 'even'

[blue]master #[/blue] (( 20%2 )) || echo 'even'
even
Note that I used to confuse even and odd. Just like in this thread on 10 May 11 9:56. Sorry, that code was for odd. For even, change the [tt]&&[/tt] to [tt]||[/tt].


Feherke.
 
Wow! Thank you very much, this makes things much clearer.

Would it not be necessary to use the an uppercase 'w' in the crontab entry though?

Best regards
 
Hi

bazil2 said:
Would it not be necessary to use the an uppercase 'w' in the crontab entry though?
Lowercase %w is completely different :
man date said:
[tt]
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
[/tt]
However, the week's order number can be calculated in different ways. I used %W because you also used it in the opening post.


Feherke.
 
Feherke, a star from me too, for going above and beyond (as usual!).

The internet - allowing those who don't know what they're talking about to have their say.
 
By the way, bazil2, it was a good idea in your original post to escape the % with a backslash, as % has special meaning to cron.

Annihilannic.
 
Hi

Annihilannic said:
By the way, bazil2, it was a good idea in your original post to escape the % with a backslash, as % has special meaning to cron.
Interesting. Not in the dcron implementation I use :
Code:
[blue]master #[/blue] man crontab | grep -c '%'
0
And neither fcron mentions % specially and as I remember neither the original cron. So which implementation are you using ?


Feherke.
 
Try man 5 crontab (man crontab only documents the editing command, not the crontab format) with vixie cron:

Code:
       The  "sixth"  field  (the rest of the line) specifies the command to be
       run.  The entire command portion of the line, up  to  a  newline  or  %
       character, will be executed by /bin/sh or by the shell specified in the
       SHELL variable of the cronfile.   [b]Percent-signs  (%)  in  the  command,
       unless escaped with backslash (\), will be changed into newline charac-
       ters, and all data after the first % will be sent  to  the  command  as
       standard input.[/b]

The same stuff paraphrased on HP-UX man crontab:

Code:
      The sixth field, command (the balance of a line including blanks in a
      crontab file), is a string that is executed by the shell at the
      specified times.  [b]A percent character (%) in this field (unless
      escaped by a backslash (\)) is translated to a newline character,
      dividing the field into "lines".  Only the first "line" (up to a % or
      end-of-line) of the command field is executed by the shell.  Any other
      "lines" are made available to the command as standard input.[/b]

Annihilannic.
 
Hi

Thank you Annihilannic. You are right, I finally found a Vixie cron where indeed all works as you wrote.

I completely forgot to check section 5 as dcron has no such man page. And neither treats % specially.

fcron has man 5 fcrontab, but not handles % in the quoted way. ( It is used for special period specifications like [tt]%nightly[/tt] . )

Thank you for the intervention. [medal]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top