Hi,
I need to schedule a script to run on the 3rd tuesday of every odd months at 9 am.
min, hour would be - 0 9
month would be - 1,3,5,7,9,11
Can someone suggest how I can schedule it to 3rd tuesday?
Thanks.
Hi,
I need to schedule a script to run on the 3rd tuesday of every odd months at 9 am.
min, hour would be - 0 9
month would be - 1,3,5,7,9,11
Can someone suggest how I can schedule it to 3rd tuesday?
Thanks.
Run it on tuesdays and test if the date is 15-21...
You could also run it each month on 15th to 21st and check if it's a Tuesday.
Here I've done the check in the crontab entry, which is nice if you would like to run the script manually and the day doesn't happen to be a Tuesday:
0 9 15-21 1,3,5,7,9,11 * [ $(date +'%w') -eq 2 ] && /path/to/your/script
Edit: On some unix versions you may have to specify the full path to date (/usr/bin/date or /bin/date).
Thanks vbe and Chubler_XL.
I just changed to run today and tested. I tried two ways:
0 9 22-31 8 2 /path/to/your/script > /path/to/your/log 2>&1
I would think that this cron should run between 22nd and 31st of a month, and since the day of the week = 2, it should run only on Tuesdays. But even if I change that to 3 it ran today.
The second way I tried is :
0 9 22-31 8 * [ $(date +'%w') -eq 2 ] && /path/to/your/script
This gives me an error:
Your "cron" job executed on Tue Aug 26 9:00:00 EDT 2014
[ $(date +'
produced the following output:
sh: test: ] missing
Not sure what I am doing wrong.
Apologise that percent sign has special meaning in the crontab file (It's use to insert a newline) and will need to be escaped your entry should look like this:
0 9 22-31 8 * [ $(date +'\%w') -eq 2 ] && /path/to/your/script
It's no good putting the 2 in the weekday field as the month and weekday are an or condition so it will run every Tuesday AND every month day from 22 to 31
Thank you soo much. This is really helpful. I used the above code and it is working as expected.
Is the && used as an AND operator? Cos when I remove it as below, the script does not execute via cron.
0 9 22-31 8 * [ $(date +'\%w') -eq 2 ] /path/to/your/script > /path/to/logfile 2>&1
&&
is Not an AND operator... Its meaning is: if TRUE( the previous test...) execute what follows, and so it make that line a true one liner. ( the contrary would be || )
Referring to the bash manpage, &&
is a "control operator":
AND and OR lists are sequences of one of more pipelines separated by
the && and || control operators, respectively. AND and OR lists are
executed with left associativity. An AND list has the form
command1 && command2
command2 is executed if, and only if, command1 returns an exit status
of zero.
Thank you for the great explanation. This has been really helpful.