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

CRON every other question

Status
Not open for further replies.

jewilson2

Technical User
Feb 7, 2002
71
0
0
US
Is there a way to have a cron job kick off a script every other thursday? I couldn't figure it out using days of the week, so I was wondering if I scheduled on an "on" thursday and then had it run on 14 day intervals or something like that.
 
A kludge way to do it would be to look at a calendar and add those entries, for example:

00 12 1 5 * /path/script
00 12 1 12 * /path/script
00 12 1 19 * /path/script
00 12 1 26 * /path/script

and continue on for each month. Of course this would create a very large crontab and would have to be changed each year.
 
Here is one way to do it...

You could set a flag file with a zero in it and have the script look at the file first, if it contains a zero then overwrite the file with a one and continue executing the script. If the script sees a one in the file then overwrite the file with zero and exit without executing the rest. Make sure the cron entry executes every Thursday.

Jim Hirschauer
 
Actually you would only need 12 lines...

Code:
00 12 1 5,19 * /path/script
00 12 2 2,16 * /path/script
00 12 3 2,16,30 * /path/script
00 12 4 14,28 * /path/script
00 12 5 11,25 * /path/script
00 12 6 8,22 * /path/script
00 12 7 7,21 * /path/script
00 12 8 3,17,31 * /path/script
00 12 9 14,28 * /path/script
00 12 10 12,26 * /path/script
00 12 11 9,23 * /path/script
00 12 12 7,21 * /path/script
 
kHz:
Are you sure about those entries? I thought the "Day of Month" was the 3rd field, and the Month was 4th.
I like Jim's idea. It would just have to be monitored in case one of the iterations didn't run for some reason.
 
You could try this
Code:
WEEK_NO=$(date +%W)
[[ $(expr 2 \* $(expr $WEEK_NO / 2)) -eq $WEEK_NO ]] || exit
/path/script

This will only run on even week numbers. I'm not sure how it will cope with the end of year, probably badly.
I actually use something like hirshaj's suggestion for a script which alternates between two action on alternate weeks.

Columb Healy
 
okay so it was late and I was waiting for some deletions to complete and wanted to leave for the day.

minute (0-59)
hour (0-23)
day of month (1-31)
month of year (1-12)
day of week (0-6 with 0=Sunday)

The only problem with the overwrite in the script is if it something does happen. To me twelve lines isn't many. And what if you usually run it the 1st and 3rd Thursday and you need to stop it from running the 3rd Thursday and let it run the 4th Thursday followed by the 1st Thursday. It would be simpler to look at a calendar and update your crontab than to remember to edit a file and change it from 0 to 1 or vice-versa; that could get confusing.
 
In the spirit of friendly debate - I really don't want to upset anyone, can I counter with
1) If the flag file is suitably named (and suitably located) then it shouldn't be hard to fix. A file called 'skip_thurs_proc.flg' is self explanatory.
2) If you have twelve lines in crontab then that's another job to do at year end when half the staff are on leave, those who are in have hangovers, and there's plenty of other end of year jobs to be completed.

All I'm saying is there's something to be said for both methods.

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top