-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!

1
  • 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. Jul 17, 2013 at 5:50

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .