Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I create a CRON job so that it runs every 15 minutes (24 hours a day) but for one week only and exits? Thanks!

share|improve this question
3  
First make a cron job to run it every 15 minutes. Second make a cron job that runs in one week and removes both cron jobs. –  David Schwartz Jul 17 '13 at 5:50
add comment

1 Answer

Cron has no concept of week, you will have to have your script or some other script make the final decision on whether to run or not. The date command can provide the ISO week of year %V so you could do something like

*/15 * * * * test `date +\%V` == "30" &&  doSomething

to run a script doSomething every 15 minutes next week but it would run next year too so use

*/15 * * * * test `date +\%G\%V` == "201330" &&  doSomething

to limit it to this year.

Note that the %'s above are escaped with a \ it is important.

share|improve this answer
    
What happen if you creat cronjob on Saturday of week 30, then on Monday of week 31, doSomething will not run? –  Gnouc Jul 17 '13 at 6:18
    
@Gnouc: Correct. –  Iain Jul 17 '13 at 6:20
1  
@Edward wants cronjob run for a week, I think it means 7 days. –  Gnouc Jul 17 '13 at 6:21
    
@Gnouc: How do you know that you're a mind reader ? I provided a method which is generally extensible. As I'm sure you know the date command is very flexible Edward could use ...+%s having pre calculated the correct number of seconds ... once you have a method the rest is easy. –  Iain Jul 17 '13 at 6:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.