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

Any ideas what's wrong with my cronjob setting? 1

Status
Not open for further replies.

bazil2

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

Can anyone tell me why my cronjob fired today (Thursday 17th)?

My intention was that it should fire at 4 minutes past 7 on the first and third Tuesday in the month.

04 07 1-7,14-21 * 2 /path/to/my/script

Best regards
 
Hi

bazil2 said:
Can anyone tell me why my cronjob fired today (Thursday 17th)?
Are you sure today is 17[sup]th[/sup] ?
bazil2 said:
My intention was that it should fire at 4 minutes past 7 on the first and third Tuesday in the month.
That condition can not be set only with crontab.
man crontab said:
If you specify both a day in the month and a day of week, the result is effectively ORd... the crontab entry will be run on the specified day of week and on the specified day in the month.
Let the command be started more times than needed, then add some conditions later.


Feherke.
 
oops - Looks like I'm 3 days in the future!

Thank you feherke, so do you have any idea what I can do to achieve my goal?


Best regards
 
Hi

If your [tt]cron[/tt] implementation allows it, you can place the condition there :
Code:
04 07 1-7,14-21 * * (( $( date +%w )==2 )) && /path/to/my/script
If not, then in your script :
Code:
[gray]#!/bin/bash[/gray]

[teal](([/teal] [navy]$([/navy] date [teal]+%[/teal]w [teal])==[/teal][purple]2[/purple] [teal]))[/teal] [teal]||[/teal] [COLOR=chocolate]exit[/color]

[gray]# ...[/gray]

Feherke.
 
(Elementary user)

Many thanks Feherke.

Have I understood correctly; basically in layman's terms, we have removed the day setting and added a date evaluation. So working from the most inside brackets outwards:

(((ask what day it is) == is the value a Tuesday) evaluate this first)

And the two '&&' means only if it is Tuesday, then execute the next script.

Did I get that right?

Best regards
 
Hi

Yes, that is correct. In shell scripting terms those are :
Code:
[red](([/red] [green]$([/green] date +%w [green])[/green]==2 [red]))[/red] [blue]&&[/blue] /path/to/my/script
[red]|[/red]  [green]|[/green]           [green]|[/green]     [red]|[/red] [blue]|[/blue]
[red]|[/red][green]command substitution[/green][red]|[/red] [blue]AND list operator[/blue]
[red]arithmetic evaluation[/red]
Not for pedantry, just to provide some keywords if you will need to search for them.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top