-2

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!

Edward
  • 115
  • 4
  • 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 05:50

1 Answers1

7

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.

user9517
  • 114,104
  • 20
  • 206
  • 289
  • What happen if you creat cronjob on Saturday of week 30, then on Monday of week 31, `doSomething` will not run? – cuonglm Jul 17 '13 at 06:18
  • @Gnouc: Correct. – user9517 Jul 17 '13 at 06:20
  • 1
    @Edward wants cronjob run for a week, I think it means 7 days. – cuonglm Jul 17 '13 at 06: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. – user9517 Jul 17 '13 at 06:31