Cron to run 3rd Tuesday of every odd months


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron to run 3rd Tuesday of every odd months
# 1  
Old 08-25-2014
Cron to run 3rd Tuesday of every odd months

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.
# 2  
Old 08-25-2014
Run it on tuesdays and test if the date is 15-21...
This User Gave Thanks to vbe For This Post:
# 3  
Old 08-25-2014
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:

Code:
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).
These 2 Users Gave Thanks to Chubler_XL For This Post:
# 4  
Old 08-26-2014
Thanks vbe and Chubler_XL.

I just changed to run today and tested. I tried two ways:

Code:
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 :
Code:
0 9 22-31 8 * [ $(date +'%w') -eq 2 ] &&  /path/to/your/script

This gives me an error:
Code:
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.

Last edited by rbatte1; 08-27-2014 at 12:31 PM.. Reason: CODE tags for output
# 5  
Old 08-26-2014
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:

Code:
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
These 2 Users Gave Thanks to Chubler_XL For This Post:
# 6  
Old 08-27-2014
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.

Code:
0 9 22-31 8 * [ $(date +'\%w') -eq 2 ] /path/to/your/script > /path/to/logfile 2>&1

# 7  
Old 08-27-2014
&& 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 || )

Last edited by vbe; 08-27-2014 at 11:54 AM.. Reason: replace successful by TRUE
This User Gave Thanks to vbe For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

cron job on 3rd sunday of every month

Hi, Actually scheduled my test scripts on test severs as shown below. They are supposed to run on 3rd sunday of every month. Unfortunately it ran on 2nd sunday of the month (suspecting that it will run every sunday). I am sorry if I miss something. Could you please let me know if I did any... (1 Reply)
Discussion started by: System Admin 77
1 Replies

2. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

3. Shell Programming and Scripting

Odd results when my script runs from cron..

Hi folks, So I wrote a script to run "top", "awk" out values fro the "top" and send the results to a data file. I then set it to run in cron every 15 minutes. Now I'm noticing that the script, and it's sub-commands are not always cleanly finishing and, in my investigations, I am also... (11 Replies)
Discussion started by: Marc G
11 Replies

4. Shell Programming and Scripting

Who -u gives different output if run from cron than if run from terminal?

If I run 'who -u' interactively or from a script invoked through bash in a tty on my Ubuntu 12LTS box I get an output like this: testuser pts/0 Dec 9 02:32 . 2163 (host.xx.yy) running the same through cron I get: testuser pts/0 2012-12-09 02:32 00:05 2163... (2 Replies)
Discussion started by: latimer
2 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Cron - job to run every 3rd Friday of the month only

Hi Expert Please help me to set a cron job schedule, Ihave a job that run every 3rd Friday of the month at 1030am. I tried to set up like this, but the job still runs every friday at 1030am. I want the job to run every 3rd Friday of the month at 1030am only 30 10 15,16,17,18,19,20,21... (2 Replies)
Discussion started by: kaibiganmi
2 Replies

6. Shell Programming and Scripting

how to schedule the cron only if tuesday doesnot fall under 5 th and 25 th of jan

Hi I am new to unix. I have to set up a cron for the program , which should run every Monday at 3 a.m, however , if the date is between the 5 th and the 15th of January,APril or December, the program should not run I know i cant do this in cron , Help with writing the if contion is... (1 Reply)
Discussion started by: saiviren
1 Replies

7. UNIX for Dummies Questions & Answers

cron does not appear to run

Hi everyone, I am having a problem with a cron, I am using Solaris 10 and need to run a php file every 5 minutes. I can run the file from the test user, but the cron does not seem to run. I do not see anything about it in /var/cron/log but see crons for other users running. What should I look... (10 Replies)
Discussion started by: atomicbits
10 Replies

8. Shell Programming and Scripting

Cron to run first day of month to calculate date 3 months ago

Hi, I would like to find out how can i calculate a date which is 3 months ago. I intend to run a cron job on the 1st of every month, and calculate the month 4 months earlier from the date. For example, if today's date is 1st May 2007, i would like to return 012007( January 2007). i can get... (1 Reply)
Discussion started by: new2ss
1 Replies

9. UNIX for Dummies Questions & Answers

Where does cron run from?

I have a script in the same directory as some files and directories im trying to tar up and I have it run in cron. Well it runs but says it can't find the directories, you need to be in the directory where the script is for it to work. Here is my cron and script its crappy but it does the trick =).... (3 Replies)
Discussion started by: kingdbag
3 Replies

10. UNIX for Dummies Questions & Answers

root cron was override w/ 3rd party software

Hi Guys, I'm new in Unix Environment. Any Unix Guru around...I need help. My question is, is it possible that the root cron could be override with 3rd party software?How can it happen. Another thing, how the cron job works?, I mean how the Unix process the cron job , I don't have an idea... (2 Replies)
Discussion started by: kupal
2 Replies
Login or Register to Ask a Question