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

(Elementary user) Can anyone ple 2

Status
Not open for further replies.

bazil2

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

Can anyone please assist in finding the problem with my crontab entry; unfortunately it results in an error when executing. The schedule runs on Tuesdays on even weeks only:

Crontab entry:
29 05 * * 2 (( $( date +'%w' )%2 )) && /path/to/my/script


Error:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file

Best regards
 
Hi

One possible problem : the error message is talking about /bin/sh, while [tt](( ))[/tt] requires more advanced shell, like Bash, Ksh, Zsh, etc.

Feherke.
[link feherke.github.com/][/url]
 
Many thanks!

Do you think also it could have something to do with those apostrophes (sorry I don't know the correct syntax term); do they look correct to you?

Best regards
 
Hi

Well, they do look correct for me, but that means less. There are several Cron implementation and they mostly added something to the original crontab syntax. From the shell's point of view it should work without any quoting :
Code:
29 05 * * 2 /usr/bin/expr $( /usr/bin/date +%w ) % 3 > /dev/null && /path/to/my/script

Personally I decided to reduce the possible clashes by keeping all code in script files. I mean I would do that test at the beginning of /path/to/my/script :
Code:
[gray]#!/bin/bash[/gray]

[teal](([/teal] [navy]$([/navy] date [teal]+[/teal][green][i]'%w'[/i][/green] [teal])%[/teal][purple]2[/purple] [teal]))[/teal] [teal]||[/teal] [b]exit[/b]

[gray]# then the real job follows...[/gray]


Feherke.
[link feherke.github.com/][/url]
 
Could you please help me on the Feherke as I think I have stumbled on another issue ....

The goal for my entry in the crontab is to execute only on Tuesday mornings in even weeks.

Looking at the man pages:
%w day of week (0..6); 0 is Sunday

%W week number of year, with Monday as first day of week (00..53)


Should I not be using the upper case 'W', mine is currently lower case?

Further, I am located in Germany, and according to the calendar here , it's week 04, however, if I run the date command, I get the following:

[root@sgi bin]# date +'%w'
2
[root@sgi bin]# date +'%W'
03
[root@sgi bin]#

Best regards



 
Hi

Sorry, I replied in moderate hurry, so I only checked the most relevant parts of my answer.

bazil2 said:
Should I not be using the upper case 'W', mine is currently lower case?
Yes, definitely not the lowercase w, but...

bazil2 said:
Further, I am located in Germany, and according to the calendar here , it's week 04
... that sounds more like ISO week number, [tt]%V[/tt].

But there is one more thing. All week numbers output by [tt]date[/tt] are formatted on 2 digit width with leading 0. And while leading 0 means octal number, look what will happen in a month :
Code:
[blue]master #[/blue] date +'%V'
04

[blue]master #[/blue] echo $(( $( date +'%V' )%2 ))
0

[blue]master #[/blue] date -d '+1 month' +'%V'
08

[blue]master #[/blue] echo $(( $( date -d '+1 month' +'%V' )%2 ))
bash: 08: value too great for base (error token is "08")

[blue]master #[/blue] date +'%[highlight]-[/highlight]V'
4

[blue]master #[/blue] echo $(( $( date -d '+1 month' +'%[highlight]-[/highlight]V' )%2 ))
0

Of course, all these using GNU [tt]date[/tt].

Feherke.
[link feherke.github.com/][/url]
 
Many thanks, no problem I see what you mean!

I'm going to try the script as below (only execute on Tuesdays in even weeks):

29 05 * * 2 /usr/bin/expr $( /usr/bin/date +%-V ) % 3 > /dev/null && /path/to/my/script

Could you please help me understand what the % 3 part means in this case?

Best regards
 
Hi

Ouch ! Sorry, the 3 was just a quick debug change to make sure the condition works. And of course, I forgot to change it back before submitting.


Feherke.
[link feherke.github.com/][/url]
 
Many thanks - that's forward thinking!

Thank you for all you help
 
Good morning,

Some feedback to my test this morning ....

I set up a cron entry to run this morning like this:


46 09 * * 3 /usr/bin/expr $( /usr/bin/date +%-V ) % 2 > /dev/null && /path/to/script

This resulted in:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `)'
/bin/sh: -c: line 1: syntax error: unexpected end of file

Do you think that it still due to the bin/sh shell; is there a way of choosing an alternative shell?

Best regards
 
Hi

Not sure, but the problem seems the be how cron is passing the command to the shell. At this point I would expect no improvement from changing the shell. Maybe changing the [tt]$( )[/tt] to [tt]` `[/tt] could solve it, as backtick characterss need no escaping in any circumstances while parenthesis sometimes need.

But I have limited experience with this part as I never had patience for it. I just move the shell code into the shell script as mentioned on 22 Jan 13 4:30 and skip all the syntax headache.


Feherke.
[link feherke.github.com/][/url]
 
Hi,

The percentage sing has special meaning in crontab files, you'll have to escape it.
Code:
46 09 * * 3 /usr/bin/expr $( /usr/bin/date +[highlight #FCE94F]\%[/highlight]-V ) [highlight #FCE94F]\%[/highlight] 2 > /dev/null && /path/to/script
Also command substitution using $() was not part of the original bourn shell which is used by cron. For portability you may want to use ` ` (althou $() should work on most modern systems).
 
Hi

Grr ! I always forget which is that special character that needs escaping. ( Because in the dcron implementation I use, no such extra escaping is required. )

Feherke.
[link feherke.github.com/][/url]
 
Hello Feherke and Stefanhei,

I thought I would let you know how this worked out ....

Despite all your efforts, I could't get this to work, hence I opened a ticket with RedHat which resulted in some interesting information pertaining to the crontab.

I'm going to try out the first suggestion.

Best regards

-------------------------------

To proceed further to answer your query I would like to say that the $(date +\%-V) \%2 part is not evaluating and thats why you are unable to run the script. (try something like this date +\%V\%3 and see if it works)
05 12 * * 1 /usr/bin/expr $(date +\%-V) \%2 > /dev/null && /path / to / my / script

Defined by the 5 fields for crontab you can use */2 for the option of every alternate of the day value (that is alternate Monday of week).

Also as you want to run it in a biweekly basis you may use date field to specify the date of every month (Best as an alternate way for example select 1st and 15th of every month)

Cron does not appear to have functionality for weekly, there is no field for weekly "every two weeks"
otherwise you would need to run a script every week, and write functionality into the script to alternately run and quit

For example, the script checks for the presence of a file such as "/.alternate-week". if the file is there, the script deletes the file and ends. if the file is not there, the script completes its task then touches "/.alternate-week" as its final task.
 
Hello Feherke and Stefanhei,

Not to be defeated, I persevered and ..... success! Here are the results for the crontab to trigger in even weeks only:

29 05 * * 1 ((10$( date +'\%V')\%2 )) > /dev/null || > /dev/null || /path / to / my /script


Now the credit must go to RH; they also had to really think before finding the solution.

Notice the '10':

It's the "0" in front that was causing the error. C will interpret it as an octal if there is a leading zero. The 10# tells it to read it as a base 10.

Best regards
Stephen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top